杭电OJ 1129(C++)
程序员文章站
2022-07-13 17:54:05
...
#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = 100;
int character[28] = { '_','a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z','.' };
char plaincode[MAXN]; //原文
char ciphercode[MAXN]; //密文
int n, k;
int index(char ch) //查找字符ch在字符表中的位置
{
for (int i = 0; i < 28; i++)
{
if (character[i] == ch)
return i;
}
}
int main()
{
while (cin >> k)
{
if (k == 0)
break;
memset(plaincode, 0, sizeof(plaincode));
memset(ciphercode, 0, sizeof(ciphercode));
cin >> ciphercode;
n = strlen(ciphercode); //密文长度
for (int i = 0; i < n; i++) //解密密文
{
plaincode[(i * k) % n] = character[(index(ciphercode[i]) + i) % 28];
}
cout << plaincode << endl;
}
return 0;
}
上一篇: 单向链表反转(含图解)