PAT Basic 1048 Digital Encryption (20 points)

Posted by brem13 on Thu, 10 Oct 2019 01:41:18 +0200

This problem requires the implementation of a digital encryption method. Firstly, a positive integer A is fixed for encryption, and every positive integer B is computed as follows: for odd digits, the digits of corresponding digits are added to the digits of A, and then 13 is redundant, where J stands for 10, Q stands for 11, K stands for 12; for even digits, the digits of B are subtracted from the digits of A, and if the result is negative, 10 is added. Let's make it number one here.

Input format:

Inputs in a row give A and B in turn, both positive integers of no more than 100 bits, separated by spaces.

Output format:

Output the encrypted result in one line.

Input sample:

1234567 368782971

Output sample:

3695Q8118
Author: CHEN, Yue
SETTING: Zhejiang University
Time limit: 400 ms
Memory limit: 64 MB

 

#include<iostream>
#include<stack>
using namespace std;
int main() {
    string s,s2;
    cin>>s>>s2;
    stack<int> sta1,sta2;
    stack<char> res;
    int tmp1,tmp2,tmpRes;
    for(int i=0;i<s.length();i++){
        sta1.push(s[i]-'0');
    }
    for(int i=0;i<s2.length();i++){
        sta2.push(s2[i]-'0');
    }
    bool isOdd=true;
    while(!sta1.empty()||!sta2.empty()){
        //Take numerical value
        if(!sta1.empty()) {
            tmp1=sta1.top();
            sta1.pop();
        }else{
            tmp1=0;
        }
        if(!sta2.empty()) {
            tmp2=sta2.top();
            sta2.pop();
        }else{
            tmp2=0;
        }
        //Parity Processing Data
        if(isOdd){
            tmpRes=(tmp1+tmp2)%13;
            switch(tmpRes){
                case 10:res.push('J');break;
                case 11:res.push('Q');break;
                case 12:res.push('K');break;
                default:res.push(tmpRes+'0');break;
            }
        }else{
            tmpRes=tmp2-tmp1;
            if(tmpRes<0)tmpRes+=10;
            res.push(tmpRes+'0');
        }
        isOdd=!isOdd;
    }
    //output
    while(!res.empty()){
        cout<<res.top();
        res.pop();
    }
    system("pause");
    return 0;
}

 

Topics: PHP