sicily 1007. To and Fro(破译密码)

来源:互联网 发布:瓷妆口红怎么样 知乎 编辑:程序博客网 时间:2024/05/02 02:23

Description:
题目大意:Mo and Larry have devised a way of encrypting messages. They first decide secretly on the number of columns and write the message (letters only) down the columns, padding with extra random letters so as to make a rectangular array of letters. For example, if the message is “There’s no place like home on a snowy night” and there are five columns, Mo would write down t o i o y h p k n n e l e a i r a h s g e c o n h s e m o t n l e w x Note that Mo includes only letters and writes them all in lower case. In this example, Mo used the character `x’ to pad the message out to make a rectangle, although he could have used any letter. Mo then sends the message to Larry by writing the letters in each row, alternating left-to-right and right-to-left. So, the above would be encrypted as toioynnkpheleaigshareconhtomesnlewx Your job is to recover for Larry the original message (along with any extra padding letters) from the encrypted one.

输入:Input will consist of two lines. The first line will contain an integer in the range 2 . ..20 indicating the number of columns used. The next line is a string of up to 200 lower case letters.

输出:giving the original plaintext message, with no spaces and a new line in the end.

样例:

输入:

3
ttyohhieneesiaabss输出:thisistheeasyoneab

Hint:
数组操作。

scanf(“%d”, &num);
char tempChar = getchar();
char str[201];
gets(str);
int len = strlen(str);
我对下面博客里的做了一些整理
http://www.cnblogs.com/joyeecheung/archive/2012/12/16/2820793.html
我是他的脑残粉·······

题解:拿到一个数字n和一串字符,如toioynnkpheleaigshareconhtomesnlewx将字符化成n个一行,即n列toioynnkpheleaigshareconhtomesnlewx偶数行倒转toioyhpknn /* 倒转 */eleairahsg /* 倒转 */econhsemot /* 倒转 */nlewx竖着打印出来theresnoplacelik

解题思路:
先将偶数行倒转
再输出,i从0到strlen(str),输出所有i%n = 0的字符,再输出所有i%n = 1的字符….到所有i%n = n-1的字符,即隔n个字符输出,直到全部字符都输出完
修改了一点点的代码

#include<stdio.h>#include<string.h>void reverse(char *s, int n ) {    int i;    char temp;    for ( i = 0; i < n / 2; i++ ) {        temp = *(s + i);        *(s + i) = *(s + n - i - 1);        *(s + n - i - 1) = temp;    }    return;}int main() {    char str[210] = {0};    int i, j, n, l;    scanf("%d", &n);    // 输入列数n    getchar();    gets(str);    l = strlen(str);    for (i = n; i < l; i = i + 2 * n )        reverse(str + i, n);        for (i = 0; i < n; i++ )            for (j = 0; j < l; j++ )            if (j % n == i )            printf("%c", str[j]);            printf("\n");return 0;}

这是源代码

#include<stdio.h>#include<string.h>void reverse( char *start, int n );int main(){    char str[210] = {0};    int i, j, n;    int len;    while ( scanf("%d", &n) && n != 0 )    {        getchar();        gets(str);        len = strlen(str);        for( i = n; i < len; i = i + 2 * n )                reverse( str + i, n );        for( i = 0; i < n; i++ )            for( j = 0; j < len; j++ )                if( j % n == i )                    printf("%c", str[j]);        printf("\n");    }    return 0;}void reverse( char *start, int n ){    int i;    char temp;    for ( i = 0; i < n / 2; i++ )    {        temp = *(start + i);        *(start + i) = *(start + n - i - 1);        *(start + n - i - 1) = temp;    }    return;}
0 0