Jack and Jill developed a special encryption method, so they can enjoy conversations without worrrying about eavesdroppers. Here is how: let L be the length of the original message, and M be the smallest square number greater than or equal to L. Add (M − L) asterisks to the message, giving a padded message with length M. Use the padded message to ?ll a table of size K × K, where K2= M. Fill the table in row-major order (top to bottom row, left to right column in each row). Rotate the table 90 degrees clockwise. The encrypted message comes from reading the message in row-major order from the rotated table, omitting any asterisks.
For example, given the
original message ‘iloveyouJack’, the message length is L = 12. Thus the padded
message is ‘iloveyouJack****’, with length M = 16. Below are the two tables
before and after rotation.
Then we read the secret message as
‘Jeiaylcookuv’.
The ?rst line of input is the
number of original messages, 1 ≤ N ≤ 100. The following N lines each have a
message to encrypt. Each message contains only characters a–z (lower and upper
case), and has length 1 ≤ L ≤ 10 000.
For each original message,
output the secret message.
1 #include <iostream>
2 #include <cstring>
3 #include <cstdio>
4 #include <cmath>
5
6 using namespace std;
7
8 int main()
9 {
10 int n;
11 int x=0;
12 int size=0;
13 double k=0;
14 int k1=0;
15 int s;
16 char a[10000];
17 char c[100][100];
18 scanf("%d",&n);
19 for(int i=0;i<n;i++){
20 cin>>a;
21 size=strlen(a);
22 k=sqrt(size);
23 k1=ceil(k);
24 s=k1*k1;
25 if(size!=s){
26 int add=s-size;
27 for(int i1=0;i1<add;i1++){
28 a[size+i1]=‘*‘;
29 }
30 a[size+add]=‘\n‘;
31 }
32 for(int j=0;j<k1;j++){
33 for(int j1=0;j1<k1;j1++){
34 c[j][j1]=a[x++];
35 }
36 }
37 x=0; //做的时候被我落掉了,结果好久才找到错误!
38 for(int r=0;r<k1;r++){
39 for(int p=k1-1;p>=0;p--){
40 if(c[p][r]!=‘*‘){
41 printf("%c",c[p][r]);
42 }
43 }
44 }
45 printf("\n");
46
47 }
48 return 0;
49 }