PAT-B1048 digital encryption

Keywords: less

1048 digital encryption (20 points)

This problem requires A digital encryption method. First, fix A positive integer A for encryption. For any positive integer B, perform the following operations on each digit of the positive integer B and the digit on the corresponding position of A: for odd digits, add the digit of corresponding digit and take the remainder on 13 - here J stands for 10, Q stands for 11 and K stands for 12; for dual digits, subtract the digit of A from the digit of B, and add 10 if the result is negative. Let's make A bit number one here.

Input format:

The input gives A and B in turn in A row, which are positive integers of no more than 100 bits, separated by spaces.

Output format:

Output encrypted results in one line.

Input example:

1234567 368782971

Output example:

3695Q8118
#include<iostream>
#include<cstring>
using namespace std;
int main(){
    string A,tB;
    cin>>A>>tB;
    int len1=A.size();
    int len2=tB.size();
    int max=len1>len2?len1:len2;
    char C[max];
    string B="";
    if(len1>len2){  //If A is longer than B, add A few zeros
        for(int i=0;i<len1-len2;i++)
            B+='0';
    }
    B+=tB;//On the left side.
    for(int i=B.size()-1,j=A.size()-1;i>=0&&j>=0;i--,j--){
        if((B.size()-i)%2==0){                  //From the right if it's even
            if(B[i]-A[j]<0){//The number of B minus the number of A is the character subtraction
                    C[i]=(B[i]-A[j])+10+'0';//Convert to character after judging whether it is less than 0
            }else{
                C[i]=B[i]-A[j]+'0';//If it is greater than 0, it will be converted to character directly
            }
        }else{  //If it's odd
            if((B[i]-'0'+A[j]-'0')%13==10)//First convert the corresponding bit into a number and then take the remainder of 13
            C[i]='J';   
            else if((B[i]-'0'+A[j]-'0')%13==11)
            C[i]='Q';   
            else if((B[i]-'0'+A[j]-'0')%13==12)
            C[i]='K';
            else {
                C[i]=(B[i]-'0'+A[j]-'0')%13+'0';
            }
        }   
    }
    for(int i=0;i<len2-len1;i++)//If a > b, assign the invariant elements
        C[i]=B[i];
    for(int i=0;i<max;i++){
        cout<<C[i];
    }   
    return 0;
}

 

Posted by Baumusu on Wed, 01 Jan 2020 03:25:07 -0800