杭电OJ 1129(C++)

本文介绍了一种基于字符替换的解密算法实现,通过输入密钥和密文,使用特定的字符表进行解密处理,最终输出原文。算法利用了字符串操作和数组查找,实现了对密文的有效解码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值