1. 程式人生 > >PAT Basic 1048

PAT Basic 1048

problem turn 輸出格式 奇數 位數 加密 進行 ont 超過

1048 數字加密

本題要求實現一種數字加密方法。首先固定一個加密用正整數 A,對任一正整數 B,將其每 1 位數字與 A 的對應位置上的數字進行以下運算:對奇數位,對應位的數字相加後對 13 取余——這裏用 J 代表 10、Q 代表 11、K 代表 12;對偶數位,用 B 的數字減去 A 的數字,若結果為負數,則再加 10。這裏令個位為第 1 位。

輸入格式:

輸入在一行中依次給出 A 和 B,均為不超過 100 位的正整數,其間以空格分隔。

輸出格式:

在一行中輸出加密後的結果。

輸入樣例:

1234567 368782971

輸出樣例:

3695Q8118

  題解:按照題意,直接寫就好O(∩_∩)O~
代碼如下:
 1 #include<iostream>
 2 #include<string>
 3 #include<cmath>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     string a, b, c;
 9     cin>>a>>b;
10     int l = 1;
11     while( a.length() > b.length()){
12         b = 0+ b;
13     }
14     while( a.length() < b.length() ){
15 a = 0 + a; 16 } 17 for(int i = a.length() - 1; i >= 0; i--,l++){ 18 if(l%2==1){ 19 int temp = (a[i] - 0 + b[i] - 0) % 13; 20 if( temp < 10 ){ 21 char d = temp + 0; 22 c = d + c; 23 } 24 else
if( temp == 10 ) 25 c = J + c; 26 else if( temp == 11 ) 27 c = Q + c; 28 else if( temp == 12 ) 29 c = K + c; 30 } 31 else{ 32 int temp = b[i] - a[i]; 33 if(temp < 0) temp += 10; 34 char d = temp + 0; 35 c = d + c; 36 } 37 } 38 cout<<c; 39 return 0; 40 }


PAT Basic 1048