HDU 1200 To and Fro

来源:互联网 发布:中国何时能拆掉网络墙 编辑:程序博客网 时间:2024/05/22 16:51

To and Fro

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


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
5toioynnkpheleaigshareconhtomesnlewx3ttyohhieneesiaabss0
 

Sample Output
theresnoplacelikehomeonasnowynightxthisistheeasyoneab
 

Source
East Central North America 2004

思路:

                   注意:这道题我是通过一位数组来做的,当然二维数组会更简单!

                   通过题目描述,我们可以知道,输入和输出之间是有一定的规律的,这个规律是通过一个列矩阵来描述的,输入是按照矩阵的s形输入的(第一次从左向右),而输出是按照列由上到下输出!

代码:

/* ***************************************************************************  **** **文件名:HDU1200201507231747**创建人:杜新新 **日  期:2015年7月23日17:17:47 **功能描述:找规律来按照确定的规律输出一串字符串 **版  本 :Dev c++  **修改人:杜新新 **修改内容:**日 期:**** ******************************************************************************   */  #include<stdio.h>#include <string.h>int main(){char a[201];int n,i,j,k,d,t;while(scanf("%d",&n)&&n){scanf("%s",a);//puts(a);d=strlen(a);for(t=0,i=0;t<n;t++)//t用来描述第t列,t和i的值是一样的,实际上只要t就行啦! {j=i;//j代表是第j列 k=0;//用k来代表是奇数行还是偶数行,奇数行时k等于0,偶数行时k等于1 for(;j<d;){printf("%c",a[j]);if(k==0)      {  j=j+2*(n-t)-1;  k=1;  }else if(k==1){j=j+1+2*t;k=0;}}i++;    }    printf("\n");}return 0;}


 

0 0
原创粉丝点击