1048 数字加密 (20point(s)

本文介绍了一种基于位操作的数字加密方法,通过正整数A与B的位运算实现加密,涉及加法、减法及取余等算术操作。文章详细解释了加密流程,包括输入格式、输出格式及示例,并提供了完整的C++代码实现。

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

本题要求实现一种数字加密方法。首先固定一个加密用正整数 A,对任一正整数 B,将其每 1 位数字与 A 的对应位置上的数字进行以下运算:对奇数位,对应位的数字相加后对 13 取余——这里用 J 代表 10、Q 代表 11、K 代表 12;对偶数位,用 B 的数字减去 A 的数字,若结果为负数,则再加 10。这里令个位为第 1 位。

输入格式:

输入在一行中依次给出 A 和 B,均为不超过 100 位的正整数,其间以空格分隔。

输出格式:

在一行中输出加密后的结果。

输入样例:

1234567 368782971

输出样例:

3695Q8118

思路 :主要难点在长短的处理,这边我是把短的补0

#include <iostream>
#include <vector>
#include <math.h>
#include <stack>
using namespace std;

int main()
{
    string s1, s2;
    vector<int> sa,sb;
    cin >> s1 >> s2;
//短的补0
    if(s1.length() != s2.length())
    {
        if(s1.length() < s2.length())
        {
            for(int i = 0; i < s2.length() - s1.length(); i++) sa.push_back(0);
            for(int i = 0; i < s1.length(); i++) sa.push_back(s1[i] - '0');
            for(int i = 0; i < s2.length(); i++) sb.push_back(s2[i] - '0');
        }
        else if(s1.length() > s2.length())
        {
            for(int i = 0; i < s1.length() - s2.length(); i++) sb.push_back(0);
            for(int i = 0; i < s2.length(); i++) sb.push_back(s2[i] - '0');
            for(int i = 0; i < s1.length(); i++) sa.push_back(s1[i] - '0');
        }
    }
    else
    {
        for(int i = 0; i < s1.length(); i++) sa.push_back(s1[i] - '0');
        for(int i = 0; i < s2.length(); i++) sb.push_back(s2[i] - '0');
    }
    int i = sa.size() - 1;
    int flag = 0;
    stack<int> output;
    while(i >= 0)
    {
        if(flag == 0)
        {
            int sum = sa[i] + sb[i];
            sum %= 13;
            if(sum < 10) output.push(sum);
            else
            {
                if(sum == 10) output.push('J');
                else if(sum == 11) output.push('Q');
                else if(sum == 12) output.push('K');
            }
            flag = 1;
            i--;
        }
        else
        {
            int ans = sb[i] - sa[i];
            if(ans < 0) ans += 10;
            output.push(ans);
            flag = 0;
            i--;
        }
    }
    while(!output.empty())
    {
        if(output.top() == 'J') cout << 'J';
        else if(output.top() == 'Q') cout << 'Q';
        else if(output.top() == 'K') cout << 'K';
        else cout << output.top();
        output.pop();
    }
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值