2013年4月_武科大程序设计大赛_解题报告(problem 1002)
2013-04-02 21:57
399 查看
To and Fro
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3384 Accepted Submission(s): 2393
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 字符串的处理首先输入一个数n,将整个字符串按照“S形”的排列方式写出,每行n个字符。然后按照竖排的顺序依次输出。可以采用直接模拟的方式把S形数组转化为从左向右,从上到下的一维数组,然后每隔n个数输出一个字符。#include<string.h> #include<stdio.h> int main() { char a[10000]; int n; char b[10000]; while(scanf("%d",&n),n) { getchar(); gets(a); int i=0,k; int length=strlen(a); int top=0; for(i=0;i<=((length-1)/n);i++) //把S形存入b数组中 { if(i%2==0) { for(k=i*n;k<=i*n+n-1;k++) b[top++]=a[k]; } else { for(k=i*n+n-1;k>=i*n;k--) { // if(!a[k]) continue; b[top++]=a[k]; } } } b[top]=0; for(k=0;k<n;k++) //每隔n-1个输出一个字符 for(int j=k;j<length;j+=n) { // if(b[j]) putchar(b[j]); } putchar(10); } return 0; }
相关文章推荐
- 2013年4月_武科大程序设计大赛(复赛)_解题报告(problem 1002)
- 2013年4月_武科大程序设计大赛(复赛-非专业组)_解题报告(problem 1002)
- 2013年4月_武科大程序设计大赛_解题报告(problem 1004)
- 2013年4月_武科大程序设计大赛(复赛-非专业组)_解题报告(problem 1004)
- 2013年4月_武科大程序设计大赛(复赛-非专业组)_解题报告(problem 1005)
- 2013年4月_武科大程序设计大赛_解题报告(problem 1005)
- 2013年4月_武科大程序设计大赛(复赛-非专业组)_解题报告(problem 1006)
- 2013年4月_武科大程序设计大赛_解题报告(problem 1006)
- 2013年4月_武科大程序设计大赛(复赛)_解题报告(problem 1001)
- 2013年4月_武科大程序设计大赛(复赛)_解题报告(problem 1003)
- 2013年4月_武科大程序设计大赛(复赛)_解题报告(problem 1004)
- 2013年4月_武科大程序设计大赛(复赛)_解题报告(problem 1005)
- 2013年4月_武科大程序设计大赛(复赛)_解题报告(problem 1006)
- 2013年4月_武科大程序设计大赛_解题报告(problem 1001)
- 2013年4月_武科大程序设计大赛(复赛-非专业组)_解题报告(problem 1001)
- 2013年4月_武科大程序设计大赛_解题报告(problem 1003)
- 2013年4月_武科大程序设计大赛(复赛-非专业组)_解题报告(problem 1003)
- 第四届吉林省大学生程序设计大赛个人解题报告 Problem B: The Farmer’s Heritage
- 第四届吉林省大学生程序设计大赛个人解题报告 Problem A: Harshad Number
- 第四届吉林省大学生程序设计大赛个人解题报告 Problem F: Coin Game