zoukankan      html  css  js  c++  java
  • PAT B1048 数字加密 (20 分)

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

    输入格式:

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

    输出格式:

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

    输入样例:

    1234567 368782971
    

    输出样例:

    3695Q8118
    #include <stdio.h>
    #include <stdlib.h>
    #include <string>
    #include <algorithm>
    #include <iostream>
    using namespace std;
    int main(){
        string s1, s2;
        getline(cin, s1);
        s2 = s1.substr(s1.find(" ") + 1, s1.length() - s1.find(" "));
        s1.erase(s1.find(" "), s1.length() - s1.find(" "));
        while (s2.length() < s1.length()){
            s2.insert(0, "0");
        }
        //cin >> s1 >> s2;
        reverse(s1.begin(), s1.end());
        reverse(s2.begin(), s2.end());
        int res;
        for (int i = 0; i < s2.length() && i < s1.length(); i++){
            if (i % 2 == 0){
                res = (s1[i] - '0' + s2[i] - '0')%13;
                if (res < 10)s2[i] = '0' + res;
                else if (res == 10)s2[i] = 'J';
                else if (res == 11)s2[i] = 'Q';
                else if (res == 12)s2[i] = 'K';
            }
            else{
                res = s2[i] - '0' - s1[i] + '0';
                if (res < 0)res += 10;
                s2[i] = '0' + res;
            }
            //cout << s2[i] << endl;
        }
        reverse(s2.begin(),s2.end());
        cout << s2;
        system("pause");
    }

    注意点:这题逻辑很简单,就是有一个坑会导致测试点2和6过不去,就是B比A短,要补0直到一样长。

    ---------------- 坚持每天学习一点点
  • 相关阅读:
    SQLSERVER 的表分区(水平) 操作记录2
    GraphQl in ASP.NET Core
    初始认知学习 .net core 逐步加深
    C# 关于使用JavaScriptSerializer 序列化与返序列化的操作
    Nginx、IIS 相关命令
    SqlServer:查询指定表所有外键关联表信息
    centos 重启宝塔命令
    c# 根据日志中的方法信息,反射再次执行相关方法
    jackson 下载地址记录
    【设计模式】六大原则
  • 原文地址:https://www.cnblogs.com/tccbj/p/10365222.html
Copyright © 2011-2022 走看看