#include <iostream>
#include <string.h>
#include <string>
using namespace std;
int main() {
int key = 0;
while(cin >> key && key != 0) {
char plaintext[71] = {'\0'}, ciphertext[71]= {'\0'};
int plaincode[71]= {0}, ciphercode[71]= {0};
int n = 0;
cin >> ciphertext;
n = strlen(ciphertext);
//完成密文数组的编码
for(int i = 0; i < n; i++) {
if(ciphertext[i] == '_')
ciphercode[i] = 0;
else if(ciphertext[i] == '.')
ciphercode[i] = 27;
else
ciphercode[i] = ciphertext[i]- 'a' + 1;
}
// 这段程序想的复杂了!!!
// for(int j = 0; j < n; j++) {
// int index = (key*j) % n;
// for(int t = 0; ; t--) {
// int res = 28 * t + ciphercode[j] +j;
// if(res >= 0 && res <= 27) {
// plaincode[index] = res;
// break;
// }
// }
// }
//已知 x = (y-k) mod 28,求 y mod 28 = (x+k) mod 28, 将密文编码数组转换成明文编码数组
for(int j = 0; j < n; j++)
plaincode[(key*j)% n] = (ciphercode[j] + j) % 28;
//将明文编码数组解码成明文字符
for(int j = 0; j < n; j++) {
if(plaincode[j] == 0)
plaintext[j] = '_';
else if(plaincode[j] == 27)
plaintext[j] = '.';
else
plaintext[j] = plaincode[j] + 'a' -1;
cout << plaintext[j];
}
cout << endl;
}
return 0;
}
zoj 1006 Do the Untwist
最新推荐文章于 2021-02-16 20:18:46 发布