Digital encryption PAT B 1048 (20 minutes)

Digital encryption 1048 (20 minutes)

This problem required to achieve a digital encryption method. First, encrypted with a fixed positive integer A, for any positive integer B, and the numbers on each of positions corresponding to 1-bit digital to A following operation: After an odd bit, the corresponding bit of the digital sum modulo 13 - - 10 used here representative of J, representative of Q 11, K 12 represents; on even bit, with the number B minus the number of a, if the result is negative, then add 10. Here a bit to make the first one.
Input formats:

Given in one row sequentially input A and B, no more than 100 are positive integers, separated by a space therebetween.
Output formats:

The output encrypted in a row.
Sample input:

1,234,567,368,782,971
sample output:

3695Q8118
Author: CHEN, Yue
units: Zhejiang University
Time limit: 400 ms
Memory Limit: 64 MB
Code length limit: 16 KB

3, but test points is an even number because the phase is reduced to 0 when 10 does not require additional
test points test points 4 and 5, but is not considered because the sum modulo odd bit is 0, then the value should be 0

#include <iostream>
#include <algorithm>
using namespace std;
int main(){
    string a,b,ans;
    int num_a,num_b;
    char odd[13]={'0','1','2','3','4','5','6','7','8','9','J','Q','K'};
    ans = "";
    cin>>a>>b;
    if(a.length() > b.length()){
        int length = a.length() - b.length();
        for (int i = 0; i < length; ++i)
            b = "0" + b;
    }
    if(a.length() < b.length()){
        int length = b.length() - a.length();
        for (int i = 0; i < length; ++i)
            a = "0" + a;
    }
    reverse(a.begin(),a.end());
    reverse(b.begin(),b.end());
    for (int i = 0; i < a.length(); ++i) {
        num_a = int(a[i])-48;
        num_b = int(b[i])-48;
        if((i+1)%2==1){
            ans += odd[((num_a+num_b)%13)];
        } else{
            if(num_b - num_a >= 0)
                ans += char(num_b - num_a + 48);
            else
                ans += char(num_b - num_a + 10 + 48);
        }
    }
    reverse(ans.begin(),ans.end());
    cout<<ans<<endl;
    return 0;
}
Published 101 original articles · won praise 50 · views 8275

Guess you like

Origin blog.csdn.net/qq_43422111/article/details/104106896