Martian Addition

Time Limit: 1 Second      Memory Limit: 32768 KB

  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
ZOJ <wbr>1205 <wbr>Martian <wbr>Addition
ZOJ <wbr>1205 <wbr>Martian <wbr>Addition
#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;
}
posted on 2011-05-11 10:11  敌敌  阅读(343)  评论(0编辑  收藏  举报