您的位置:首页 > 其它

HDOJ HDU 1129 Do the Untwist

2017-09-12 20:41 232 查看

HDOJ 1129 Do the Untwist

题目

点此查看 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

Array012
ciphertext‘c’’s’‘.
ciphercode31927
plaincode3120
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  密码 HDOJ