HDOJ HDU 1129 Do the Untwist

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;
    }
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值