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
KEY:这题其实不是很难我居然写了两个多小时啊,郁闷啊,是我自己太不认真了啊,这题考的是大整数的加法和20进制的转换;从最后以为加上去,注意是否产生新位;
Source:
#include < iostream >
using namespace std;
int a[ 150 ];
int k;
void init()
... {
for(int i=0;i<150;i++)
a[i]=0;
k=0;
}
int translate( char c)
... {
if(c>='0'&&c<='9') return c-48;
else return c-87;
}
void add( char ch1[], char ch2[])
... {
int la=strlen(ch1);
int lb=strlen(ch2);
int i=la-1,j=lb-1;
int t1,t2,t;
while(i>=0&&j>=0)
...{
t1=translate(ch1[i]);
t2=translate(ch2[j]);
t=t1+t2;
a[k]+=t;
a[k+1]+=a[k]/20;
a[k]=a[k]%20;
k++;
i--;
j--;
}
if(i>=0)
...{
while(i>=0)
...{
a[k]+=translate(ch1[i]);
a[k+1]+=a[k]/20;
a[k]=a[k]%20;
k++;
i--;
}
}
if(j>=0)
...{
while(j>=0)
...{
a[k]+=translate(ch2[j]);
a[k+1]+=a[k]/20;
a[k]=a[k]%20;
k++;
j--;
}
}
}
void output()
... {
int i;
if(a[k]!=0) i=k;
else i=k-1;
for(;i>=0;i--)
...{
if(a[i]>9)
...{
char c=a[i]+87;
cout<<c;
}
else cout<<a[i];
}
cout<<endl;
}
int main()
... {
// freopen("fjnu_1865.in","r",stdin);
char ch1[150],ch2[150];
while(cin>>ch1>>ch2)
...{
init();
add(ch1,ch2);
output();
}
return 0;
}