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

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列
toioy
nnkph
eleai
gshar
econh
tomes
nlewx

偶数行倒转
toioy
hpknn /* 倒转 */
eleai
rahsg /* 倒转 */
econh
semot /* 倒转 */
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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值