PAT B1048

来源:互联网 发布:php大牛的博客 编辑:程序博客网 时间:2024/06/08 01:26

1048. 数字加密(20)

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

输入格式:

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

输出格式:

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

输入样例:
1234567 368782971
输出样例:
3695Q8118
代码如下:

#include <iostream>#include <cmath>#include <cstring>using namespace std;char k[13] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'J', 'Q', 'K'};char s[110], s1[110], s2[110];void _reverse(char * a){    char ch;    for(int i = 0, j =  strlen(a) - 1; i <= j; i++, j--){        ch = a[i];        a[i] = a[j];        a[j] = ch;    }}int main(){    bool kkk = true;    cin>>s>>s1;    _reverse(s);    _reverse(s1);    int _max, temp;    _max = max(strlen(s), strlen(s1));    char ch;    for(int i = 0; i < _max; i++){        int nums = i < strlen(s) ? s[i] - '0' : 0;        int nums1 = i < strlen(s1) ? s1[i] - '0' : 0;        if((i % 2) == 0) {            ch = k[(nums + nums1) % 13];        }else{            temp = nums1 - nums;            if(temp < 0) temp += 10;            ch = k[temp % 13];        }        s2[i] = ch;    }    for(int i = _max - 1; i >= 0; i--){        cout<<s2[i];    }    return 0;}



0 0
原创粉丝点击