eden破解密文

 题目名称

破解密文

题目描述

题目大意: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 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.

输出:giving the original plaintext message, with no spaces and a new line in the end.

样例

  输入:

3
ttyohhieneesiaabss

输出:
thisistheeasyoneab

 HINT

数组操作。

scanf("%d", &num);
char tempChar = getchar();
char str[201];
gets(str);
int len = strlen(str);

题目难度

      0

我的理解

      给一串信息进行加密处理,加密方式是将该串信息以矩阵一列一列输入,如果矩阵没有满则随机补充到最后形成一个完整的矩阵,然后以一列一列蛇形输出(即第一行从左到右第二行从右到左,第三行又从左到右依次类推),输出的数字串即为加密后的密码。

      举个例子:需要对xiaoyubei进行加密,则输入变成矩阵xob一列一列地输入,刚好填满,然后蛇形输出xobeyiaui那么这个得到的就是密码。

                                                                            iye

                                                                            aui

      题目给定输入是加密后的密码,需要你进行输出原本的信息;即输入了xobeyiaui让我们转换为xiaoyubei。

方法&解释

      这一题我就直接以二维数组a来储存输入并且输入以按照它加密的矩阵形式排列,关键在输入成为这样一个矩阵,那么输出即直接一列一列输出即可;

      我直接用i表示输入列数,循环从1->n,j表示行数,最开始为1,每一次i==n时,j++,说明一行结束,输入储存a数组时要判断j是奇数还是偶数,如果j为奇数则按照从左到右的顺序,如果j是偶数则按照从右往左的顺序,最后矩阵完成,要以' '结束判断,原本应该是''题目所给的,我也不知道为什么,等下我再查一查,修改一下,要注意最开始输入n的时候有一个回车,这个字符要消除掉,否则会影响后面的输入。

我的代码

 1 #include<stdio.h>
 2 int main() {
 3     int n;
 4     scanf("%d", &n);
 5     char a[20][20] = {0};
 6     char t;
 7     scanf("%c", &t);
 8     int i = 1, j = 1, k = 0;
 9     while (t != '') {
10         if (k == 1) break;
11         for (i = 1; i <= n; i++) {
12         scanf("%c", &t);
13         if (t != '
') {
14             if (t < 'a') {
15                 t = t + 'a' -'A';
16             }
17             if (j % 2 == 0) {
18                 a[j][n - i + 1] = t;
19             } else {
20                 a[j][i] = t;
21             }
22         } else {
23             k = 1;
24             break;
25         }
26         if (i == n) j++;
27         }
28     }
29     int row = j - 1;
30     for (j = 1; j <= n; j++) {
31         for (i = 1; i <= row; i++) {
32             printf("%c", a[i][j]);
33         }
34     }
35     printf("
");
36 }

标程代码

 1 /*
 2  * this is the direct but stupid way. you can improve it by yourself by using one array.
 3  */
 4  
 5 #include<stdio.h>
 6 #include<string.h>
 7  
 8 char tmp[101][21];
 9 char temp[101][21];
10  
11 int main() {
12     int num, i, j;
13     // for input.
14     scanf("%d", &num);
15     char tempChar = getchar();
16     char str[201];
17     gets(str);
18     int len = strlen(str);
19     int row = len/num;
20     for (i = 0; i < len; i++) {
21         tmp[i/num][i%num] = str[i];
22     }
23     // for processing.
24     for (i = 0; i < row; i++) {
25         for (j = 0; j < num; j++) {
26             if (i%2 == 1) temp[i][j] = tmp[i][num - j - 1];
27             else temp[i][j] = tmp[i][j];
28         }
29     }
30     // for output.
31     for (i = 0; i < num; i++) {
32         for (j = 0; j < row; j++) {
33             printf("%c", temp[j][i]);
34         }
35     }
36     printf("
");
37     return 0;
38 }

标程解释

原文地址:https://www.cnblogs.com/iamxiaoyubei/p/5032456.html