HDU 1200 To and Fro

/*
中文题目  来回
中文翻译-大意  说一个人给别人写信,但将信加密了,譬如他说了如下的话:There’s no place like home on a snowy night。将它加密成如题目的形式,有五列。这只是加密的形式,还要将它用信的形式呈现出来,所以要将它再写出来,不然会让别人一眼就看出来了,还加密个毛线啊!他写出来的形式是,先从左到右,再从右到左的顺序将每个字母写到信中去。你的任务是将数加密的信读取出来。
题目大意:解密一封信
解题思路:先设一个字符型的二维数组,将读取的字符按照题目要求存入数组中,再将它输出就可以了。
难点详解:在将字符存入二维数组时,如何存,就是一个问题。要先判断行数的奇偶性,这样有利于输入字符的顺序。还有一点就是在倒着存数时,只动二维数组,一位数组不懂,这样有利于存数。存数时,要思路清晰,要细心,不然很容易出错,还会浪费宝贵的时间。
关键点:思路清晰后,存数时,就要加倍细心,不然很容易出错的
解题人:lingnichong
解题时间:2014-08-16 11:22:07
解题体会:一开始在存数的时候,老是思路不清晰,老是将两个数组都变化,结果数不出结果,后来静下来,整理了一下思路,才写了下来。搞了我老长时间,但也收获不少经验。下次争取不犯这样的错误。写题一定要思路清晰,不可似是而非。在存数时,头脑一定要清醒啊!

*/

To and Fro

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4654    Accepted Submission(s): 3249


Problem 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
There will be multiple input sets. Input for each set 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. The last input set is followed by a line containing a single 0, indicating end of input.
 

Output
Each input set should generate one line of output, giving the original plaintext message, with no spaces.
 

Sample Input
  
  
5 toioynnkpheleaigshareconhtomesnlewx 3 ttyohhieneesiaabss 0
 

Sample Output
  
  
theresnoplacelikehomeonasnowynightx thisistheeasyoneab
 

Source
 

#include<stdio.h>
#include<string.h>
char a[25][250],str[300];
int  main()
{
    int t;
    int len,i,j;
    int n,m,k;
    while(scanf("%d",&t),t)
    {
        getchar();
        n=m=k=0;
        memset(a,'\0',sizeof(a));//将字符数组初始化
        gets(str);
        len=strlen(str);
        m=len/t;//有多少行 
        for(i = 0; i < m && k < len; i++)//将一开始输入的字符按照先从左到右,再从右到左填入二维数组中
        {
            for(j = 0; j < t; j++)
            {
                if(i%2)//判断是奇数行
                    a[i][t-j-1] = str[k++];
                else
                    a[i][j] = str[k++];
            }
        }
        for(i=0;i<t;i++)//按顺序输出就可以了,注意是从上到下输出
            for(j=0;j<m;j++)
            {
                printf("%c",a[j][i]);
                
            }
        printf("\n");    
    } 
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值