Input:
You're given several pairs of Martian numbers, each number on a line.
Martian number consists of digits from 0 to 9, and lower case letters from a to j (lower case letters starting from a to present 10, 11, ..., 19).
The length of the given number is never greater than 100.
Output:
For each pair of numbers, write the sum of the 2 numbers in a single line.
Sample Input:
1234567890 abcdefghij 99999jjjjj 9999900001
Sample Output:
bdfi02467j iiiij00000
#include <iostream>
#include <map>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
map<char, int > char_int;
for(int i=0;i< 10 ;i++){
char_int['0'+i]=i;
}
for(int i=0; i< 10;i++){
char ch='a';
char_int[ch+i]=i+10;
}
map<int, char > int_char;
for(int i=0;i< 10 ;i++){
int_char[i]='0'+i;
}
for(int i=0; i< 10;i++){
char ch='a';
int_char[i+10]=ch+i;
}
string num1, num2 ,num3;
while(cin>>num1>>num2){
reverse(num1.begin(),num1.end());
reverse(num2.begin(),num2.end());
int up=0;
num3="";
string str;
if(num1.size()< num2.size()){
str = num1;
num1= num2;
num2= str ;
}
for(int i=0;i<num2.size();i++){
if(char_int[ num1[i] ]+ char_int[num2[i]]+ up>= 20){
num3=int_char[char_int[ num1[i] ]+ char_int[num2[i]]+ up- 20]+ num3;
up=1;
}
else {
num3=int_char[char_int[ num1[i] ]+ char_int[num2[i]]+ up]+ num3;
up=0;
}
}
if(up==1){
for(int i= num2.size();i<num1.size();i++){
if(char_int[num1[i] ]+ up>=20){
num3=int_char[char_int[num1[i]]+up-20]+num3;
up=1;
}
else{
num3=int_char[char_int[ num1[i] ]]+num3;
up=0;
}
}
}
else {
for(int i= num2.size();i<num1.size();i++){
num3=int_char[char_int[ num1[i] ]]+num3;
}
}
if(up==1){
num3='1'+num3;
}
cout<<num3<<endl;
}
return 0;
}