本文主要是介绍1007. To and Fro,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
【题目要求】
1007. To and Fro
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
Mo and Larry have devised a way of encrypting messages. They first decide secretly on the number ofcolumns and write the message (letters only) down the columns, padding with extra random letters soas to make a rectangular array of letters. For example, if the message is "There's no place like home ona snowy night" and there are five columns, Mo would write downt o i o yh p k n ne l e a ir a h s ge c o n hs e m o tn l e w xNote that Mo includes only letters and writes them all in lower case. In this example, Mo used thecharacter `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 andright-to-left. So, the above would be encrypted astoioynnkpheleaigshareconhtomesnlewxYour job is to recover for Larry the original message (along with any extra padding letters) from theencrypted one.
Input
There will be multiple input sets. Input for each set will consist of two lines. The first line will containan integer in the range 2 . ..20 indicating the number of columns used. The next line is a string of upto 200 lower case letters. The last input set is followed by a line containing a single 0, indicating end ofinput.
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、读题出现问题,然后google原题,看了题目中给的示例矩阵,才明白是明文竖着排,之前因为觉得竖着排需要数,就以为是明文横着排
2、编程时应该先默认输入一个样例,这样就不用一遍遍输入同样的样例了。
3、一开始对一个样例进行编程,不免会有默认的错误,就需要时刻注意和多个样例的测试。
4、对于如何输入多个样例,while居然没调出来,不知道为什么,所以就用了for,看着挺蛋疼。
#include<iostream>
#include <string>
using namespace std;int main ()
{int column =1 ;string ciphertext ;cin>>column;for(;column!=0;)
{cin>>ciphertext;int row = ciphertext.size()/column;string rectangle[row][column];int cinCol = -1;for(int i=0;i<ciphertext.size();i++){if(cinCol == 2*column-1)cinCol = -1;cinCol++;if(cinCol<column)rectangle[i/column][cinCol] = ciphertext[i];else rectangle[i/column][2*column-1-cinCol] = ciphertext[i];//cout<<"rectangle[][]"<<i/column<<" "<<cinCol<<" "<<ciphertext[i]<<endl;}for (int i=0;i<ciphertext.size();i++){cout<<rectangle[i%row][i/row];}cout<<endl;cin>>column;
}system ("pause");return 0;
}
这篇关于1007. To and Fro的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!