zoukankan      html  css  js  c++  java
  • 1048. 数字加密(20)

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

    输入格式:

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

    输出格式:

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

    输入样例:

    1234567 368782971
    

    输出样例:

    3695Q8118
    #include<cstdio>
    #include<cstring>
    const int maxn = 110;
    char str1[maxn],str2[maxn],ans[maxn] = {0};
    void reverse(char str[]){
        int len = strlen(str);
        for(int i = 0; i < len/2; i++){
            int temp = str[i];
            str[i] = str[len - i - 1];
            str[len - i - 1] = temp;
        }
    }
    int main(){
        scanf("%s %s",str1,str2);
        reverse(str1);
        reverse(str2);
        int len1 = strlen(str1);
        int len2 = strlen(str2);
        int len = len1 > len2 ? len1 : len2;
        int i;
        for(i = 0; i < len ; i++){
            int numA = i < len1 ? str1[i] - '0' : 0; //如果i<len1 numA=str[i] 否则 numA=0 
            int numB = i < len2 ? str2[i] - '0' : 0;
            //int numA = str1[i] - '0';
            //int numB = str2[i] - '0';
            int temp;
            if(i % 2 == 0) { //数字奇数位 str[0] 
                temp = (numA + numB) % 13;
                if(temp == 10) ans[i] = 'J';
                else if(temp == 11) ans[i] = 'Q';
                else if(temp == 12) ans[i] ='K';
                else ans[i] = temp +'0';
            } else{
                temp = numB - numA;
                if(temp < 0) temp = temp + 10;
                ans[i] = temp + '0';
            }
        }
        reverse(ans);
        puts(ans);
        return 0;
    }
  • 相关阅读:
    django–url
    SQLServer-镜像配置
    linux-阿里云ECS部署PPTP(centos)
    linux- svn服务器
    python(7)– 类的反射
    python(7)–类的多态实现
    python(6)-shutil模块
    python(6)-执行shell命令
    python(6)-类
    nagios–配置文件
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/8547910.html
Copyright © 2011-2022 走看看