PAT Class B-Digital Encryption

Title description
This question requires the realization of a digital encryption method.

First, fix a positive integer A for encryption. For any positive integer B, perform the following operations on each digit in the corresponding position of A:

  • For odd digits, add the numbers of the corresponding digits and take the remainder of 13-here J stands for 10, Q stands for 11, and K stands for 12;
  • For even digits, subtract the number of A from the number of B. If the result is negative, add another 10.

Let the ones place be the first place here.

Input format The
input is given in sequence of A and B on one line, both of which are positive integers with no more than 100 digits, separated by spaces.

Output format
Output the encrypted result in one line.

Input sample
1234567 368782971

Sample output
3695Q8118


answer:

解题思路: Comparison of A、Bthe length in the short who who make up the front 前导0;

#include <iostream>
using namespace std;

int main()
{
    
    
	string A, B;
	cin >> A >> B;
	
	int n = A.size(), m = B.size();
	
	if(n > m)
	{
    
    
		int cnt = n - m;
		while(cnt --) B = '0' + B;
	}
	else
	{
    
    
		int cnt = m - n;
		while(cnt --) A = '0' + A;
		n = m;
	}

	for (int i = n - 1, cnt = 1; i >= 0; i --, cnt ++) 
	{
    
    
		if(cnt % 2) 
		{
    
    
			int sum = 0;
			sum = ((A[i] - '0') + (B[i] - '0')) % 13;
			char c = sum + '0';
			if(sum == 10) c = 'J';
			if(sum == 11) c = 'Q';
			if(sum == 12) c = 'K';
			B[i] = c;
		}
		else 
		{
    
    
			int d = (B[i] - '0') - (A[i] - '0');
			if(d < 0) d += 10;
			B[i] = d + '0';
		}
	}
	
	cout << B << endl;
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_46239370/article/details/114023112