//這一題主要用到map映射器!要注意進位的處理就可以了!到相加到最後,還需要注意是否有進位!#include "iostream"#include "map"#include "string"#include "algorithm"using namespace std;int main(){map<char, int> m;map<int, char> mm;char ch1 = '0' ,ch2 = 'a';string str1, str2;//給map容器的賦值for (int i = 0; i < 10; i++)m.insert(pair<char, int>(ch1+i, i));for (int i = 10, j = 0; i < 20; i++, j++)m.insert(pair<char, int>(ch2+j, i));for (int i = 0; i < 10; i++)mm.insert(pair<int, char>(i, ch1+i));for (int i = 10, j = 0; i < 20; i++, j++)mm.insert(pair<int, char>(i, ch2+j));while (cin >> str1 >> str2){int length1, length2, temp, carry = 0;string ans = "";length1 = str1.size();length2 = str2.size();if (length1 >= length2)//分兩種情況進行處理!{for (int i = length1-1, j = length2-1; j >=0 ; i--, j--){temp = m[str1[i]] + m[str2[j]] + carry;if (temp >= 20)//判斷是否需要進位{carry = temp / 20;temp %= 20;}elsecarry = 0;ans += mm[temp]; }for (int i = length1-length2-1; i >= 0 ; i--)//當兩個字串的長度不相等的時候,還需要處理長字串剩下的部分!{if (carry == 0)ans += str1[i];else{int temp1;temp1 = m[str1[i]] + carry;if (temp1 >= 20){carry = temp1 / 20;temp1 %= 20;}elsecarry = 0;ans += mm[temp1];}}if (carry != 0)//如果最後還有進位的,還需要添加到最後的結果中ans += carry + 48;}else//這種情況和上面的做法一樣,只是字串的長度所影響!{for (int i = length1-1, j = length2-1; i >=0 ; i--, j--){temp = m[str1[i]] + m[str2[j]] + carry;if (temp >= 20){carry = temp / 20;temp %= 20;}elsecarry = 0;ans += mm[temp]; }for (int i = length2-length1-1; i >= 0 ; i--){if (carry == 0)ans += str2[i];else{int temp1;temp1 = m[str2[i]] + carry;if (temp1 >= 20){carry = temp1 / 20;temp1 %= 20;}elsecarry = 0;ans += mm[temp1];}}if (carry != 0)ans += carry + 48;}reverse(ans.begin(), ans.end());//反轉字串輸出!cout << ans << endl;}