Description
In the 22nd Century, scientists have discovered intelligent residents live on the Mars.
Martians are very fond of mathematics. Every year, they would hold an Arithmetic
Contest on Mars (ACM). The task of the contest is to calculate the sum of two 100-digit numbers, and the winner is the one who uses least time.
This year they also invite people on Earth to join the contest.
As the only delegate of Earth, you're sent to Mars to demonstrate the power of mankind.
Fortunately you have taken your laptop computer with you which can help you do the job
quickly. Now the remaining problem is only to write a short program to calculate the sum
of 2 given numbers. However, before you begin to program,
you remember that the Martians use a 20-based number system as they usually have 20 fingers.
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
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 char a[105],b[105]; 7 int r[105]; 8 int s(char d) 9 { 10 if(d>='0'&&d<='9') 11 return d-'0'; 12 else if(d>='a'&&d<='j') 13 return d-'a'+10; 14 } 15 char q(int i) 16 { 17 if(i>=0&&i<=9) 18 return '0'+i; 19 else if(i>=10&&i<=19) 20 return 'a'+i-10; 21 } 22 int main() 23 { 24 int len1,len2,len3; 25 int i,j; 26 char c; 27 while(cin>>a>>b) 28 { 29 len1=strlen(a); 30 len2=strlen(b); 31 memset(r,0,sizeof(r)); 32 for(i=0;i<len1/2;i++) 33 { 34 c=a[i]; 35 a[i]=a[len1-1-i]; 36 a[len1-1-i]=c; 37 } 38 for(i=0;i<len2/2;i++) 39 { 40 c=b[i]; 41 b[i]=b[len2-1-i]; 42 b[len2-1-i]=c; 43 } 44 for(i=0;i<min(len1,len2);i++) 45 { 46 r[i]=s(a[i])+s(b[i]); 47 } 48 for(i=min(len1,len2);i<max(len1,len2);i++) 49 { 50 if(len1>len2) 51 r[i]=s(a[i]); 52 else 53 r[i]=s(b[i]); 54 } 55 len3=i; 56 for(i=0;i<len3;i++) 57 { 58 if(r[i]>=20) 59 { 60 r[i+1]++; 61 r[i]-=20; 62 } 63 } 64 if(r[len3]!=0) 65 len3++; 66 for(i=len3-1;i>=0;i--) 67 cout<<q(r[i]); 68 cout<<endl; 69 } 70 return 0; 71 }