HDOJ 1129 Do the Untwist
题目
分类
模拟
题意
模拟解密
字幕数字对照 “_” = 0, “a” = 1, “b” = 2,.., “z” = 26, 和 “.” = 27。
给出 k
计算公式 ciphercode[i] = (plaincode[ki mod n] - i) mod 28
解读一下公式
设 解密到 第i个字符(共 m 个字符)
意思是 明文的 第 (i *k) %m个字符 = (密文字母对应得数字 + i )% 28 再查表例如 k = 5
Array 0 1 2 ciphertext ‘c’ ’s’ ‘. ciphercode 3 19 27 plaincode 3 1 20 plaintext ‘c’ ‘a’ ‘t’ 第 (0 * k) % 3 = 0 个字符 (c + 0)% 28 = 3 表中 3 还是 ‘c’
第 (1 * 5) % 3 = 2 个字符 (s + 1)% 28 = 20 表中 20 是 ‘t’第 (2 * 5) % 3 = 1 个字符 (. + 2)% 28 = 1 表中 1 是 ‘a’
解密完成
题解
模拟即可
代码
#include <iostream>
#include <cstring>
#define maxn 100
#define maxt 28
using namespace std;
int tab[maxt] = {'_','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];
int m,k;
char ciphercode[maxn];
int findposition(char ch);
int dectypto();
int main()
{
while(cin >> k && k)
{
memset(plaincode,0,sizeof(plaincode));
memset(ciphercode,0,sizeof(ciphercode));
cin >> ciphercode;
m = strlen(ciphercode);
dectypto();
cout << plaincode << endl;
}
return 0;
}
int dectypto()
{
for(int i = 0;i < m;i++)
{
plaincode [(i * k) % m] = tab[(findposition(ciphercode[i]) + i) % maxt];
}
return 0;
}
int findposition(char ch)
{
for(int i = 0;i < maxt;i++)
{
if(tab[i] == ch)
return i;
}
}