码迷,mamicode.com
首页 > 其他好文 > 详细

To and Fro(字符串水题)

时间:2015-06-07 00:55:20      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:

To and Fro

点我

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
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 int main()
 6 {
 7     int i,n;
 8     char ch;
 9     char a[11][21];
10     char b[1000];
11     bool flag=0;
12     int p=0,j=0;
13     while(cin>>n&&n)
14     {        
15         int k=0;
16         memset(b,0,sizeof(b));
17         memset(a,0,sizeof(a));
18         getchar();
19         gets(b);
20         int len=strlen(b);
21         while(k<=len)
22         {
23             if(!flag)
24             {
25                 for(i=0;i<n;i++)
26                     if(k<=len)
27                         a[p][i]=b[k++];
28                 flag=1;
29                 p++;
30                 continue;
31                 
32             }    
33             if(flag)
34             {
35                 for(i=n-1;i>=0;i--)
36                     if(k<=len)
37                         a[p][i]=b[k++];
38                 flag=0;
39                 p++;
40                 continue;
41             }
42         }
43         for(i=0;i<n;i++)
44         {
45             for(j=0;j<p-1;j++)
46             {
47                 printf("%c",a[j][i]);
48             }
49         }
50         cout<<endl;
51         p=0;
52     }
53 }

 

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 int main()
 4 {
 5     char c,s[120][30];
 6     int i,j,k,n,f,count;
 7     while(scanf("%d",&n),n){
 8         f=count=0;
 9         for(k=1;f==0;k++)
10             for(i=1;i<=n&&f==0;i++){
11                 scanf("%c",&c);
12                 if(c==\n) f=1;
13                 else{
14                     count++;
15                     if(k%2==1) s[k][i]=c;
16                     else s[k][n-i+1]=c;
17                 }
18             }
19         for(i=1;i<=n;i++)
20             for(j=1;j<=count/n;j++)
21                 putchar(s[j][i]);
22         putchar(\n);
23     }
24     return 0;
25 }

 

To and Fro(字符串水题)

标签:

原文地址:http://www.cnblogs.com/a1225234/p/4557505.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!