Dowemo

Topic:

It's required to implement a digital encryption method. Fi t fixed an encryption with positive integer a, for any positive integer b, the number of each 1 digit and a corresponding position of a. The following operatio are performed: For odd bits, the numbers of the corresponding bits are added to 13. The j is 10, and q stands for 11, and k stands for 12. Dual digit, minus a digit with b, if the result is negative, add 10. Here's a bit bit bit 1.

format:

Enter a and b in a row to indicate a positive integer that isn't exceeding 100 bits, separated by space.

output format:

Output encrypted results on a single line.

Enter the sample:

1234567 368782971






Output sample:

3695Q8118






思路:






1. 首先把短的数字补齐,然后按要求计算;






2. 注意如果开始先把字符串反转则只需要补齐b,a不影响计算;






3. 但如果不反转则都要补齐,原因是在本例中用到了a[l1-i-1]和a[l1-i-1]这样的计算,补不齐无法用统一形式表达多种情况。








#include<iostream>


#include<string>


using namespace std;


int main() {


 string a, b;


 cin>>a>>b;


 char str[13] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'J', 'Q', 'K'};


 int l1, l2, l, i, m, d;


 l1 = a.length();


 l2 = b.length();


 if (l2 <l1) {


 d = l1 - l2;


 for (i=0; i<d; i++)


 b = '0' + b;


 }


 if (l1 <l2) {


 d = l2 - l1;


 for (i=0; i<d; i++)


 a = '0' + a;


 l1 = l2;


 }


 for (i=0; i<l1; i=i+2) {


 m = (b[l1-1-i] - '0' + a[l1-1-i] - '0') % 13;


 b[l1-1-i] = str[m];


 }


 for (i=1; i<l1; i=i+2) {


 m = b[l1-1-i] - a[l1-1-i];


 b[l1-1-i] = str[(m + 10) % 10];


 }


 cout<<b;


 return 0;


}












Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs