To and Fro
来源:互联网 发布:数控编程软件哪个好 编辑:程序博客网 时间:2024/05/22 13:47
To and Fro
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 24 Accepted Submission(s) : 15
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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.
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
#include <iostream>using namespace std;int main(){ int i,k,n,j; cin>>k; while (k!=0) { char a[20][200]= {0}; string s; cin>>s; n=s.length(); for (i=0; i<=n; i++) { if (i/k%2==0) a[i/k][i%k]=s[i]; else a[i/k][k-i%k-1]=s[i]; } for (j=0; j<k; j++) for (i=0; i<n/k; i++) cout<<a[i][j]; cout<<endl; cin>>k; } return 0;}
或者
#include<stdio.h>#include<string.h>int main(){ int i,j,k,n,len; char str[10000],str1[105][105]; while(scanf("%d",&n),n) { getchar(); gets(str); len=strlen(str); k=0; for(i=0;i<len/n;i++) { if(i%2==0) { for(j=0;j<n;j++) str1[i][j]=str[k++]; } else { for(j=n-1;j>=0;j--) str1[i][j]=str[k++]; } } for(j=0;j<n;j++) { for(i=0;i<len/n;i++) printf("%c",str1[i][j]); } if(n==0) break; else printf("\n"); } return 0;}
- sicily1007. To and Fro
- 1007. To and Fro
- 1007. To and Fro
- zoj2208--------------------To and Fro
- 1007. To and Fro
- To and Fro
- To and Fro
- HDU1200:To and Fro
- 1007[To and Fro]
- To and Fro
- 1007. To and Fro
- sicily1007. To and Fro
- hdu_1200 To and Fro
- 1547. To and Fro
- HDU1200 To and Fro
- 1007. To and Fro
- 1200 To and Fro
- To and Fro
- hdu1337-The Drunk Jailer
- 提升速度的几个环节
- 英语面试题整理
- java 程序的运行机制
- 将一个数组中的奇元素全部移到数组的前半部分,即将奇偶元素分开
- To and Fro
- Android标准的Button的selector
- 转:大众点评网产品类 校招题目解析
- opencv中的Mat
- zip和*在python中的具体例子
- 基于visual c++之windows核心编程代码分析(56)使用winsock搜索蓝牙设备
- C++ Primer 中文版 学习笔记(九)
- 2013 - ECJTU 暑期训练赛第三场-problem-K
- HTTP 协议详解