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
WA了n次,总是会漏考虑某各方面
AC是用了一段非常冗长的程序
如下:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
//倒置数组函数
void fun(char a[],int n) //n为数组长度
{
int i;
char t;
for(i=0;i<n/2;i++)
{
t=a[i];
a[i]=a[n-1-i];
a[n-1-i]=t;
}
}
//交换数组函数
void sw(char a[],char b[],int l) //l为最长数组的长度
{
int i;
char t;
for(i=0;i<l;i++)
{
t=a[i];
a[i]=b[i];
b[i]=t;
}
}
//change to num
void CTN(char a[],int b[],int n)
{
int i;
for(i=0;i<n;i++)
{
switch(a[i])
{
case '0':
b[i]=0; break;
case '1':
b[i]=1; break;
case '2':
b[i]=2; break;
case '3':
b[i]=3; break;
case '4':
b[i]=4; break;
case '5':
b[i]=5; break;
case '6':
b[i]=6; break;
case '7':
b[i]=7; break;
case '8':
b[i]=8; break;
case '9':
b[i]=9; break;
case 'a':
b[i]=10; break;
case 'b':
b[i]=11; break;
case 'c':
b[i]=12; break;
case 'd':
b[i]=13; break;
case 'e':
b[i]=14; break;
case 'f':
b[i]=15; break;
case 'g':
b[i]=16; break;
case 'h':
b[i]=17; break;
case 'i':
b[i]=18; break;
case 'j':
b[i]=19; break;
}
}
}
void outp(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
switch(a[i])
{
case 0:
cout<<"0"; break;
case 1:
cout<<"1"; break;
case 2:
cout<<"2"; break;
case 3:
cout<<"3"; break;
case 4:
cout<<"4"; break;
case 5:
cout<<"5"; break;
case 6:
cout<<"6"; break;
case 7:
cout<<"7"; break;
case 8:
cout<<"8"; break;
case 9:
cout<<"9"; break;
case 10:
cout<<"a"; break;
case 11:
cout<<"b"; break;
case 12:
cout<<"c"; break;
case 13:
cout<<"d"; break;
case 14:
cout<<"e"; break;
case 15:
cout<<"f"; break;
case 16:
cout<<"g"; break;
case 17:
cout<<"h"; break;
case 18:
cout<<"i"; break;
case 19:
cout<<"j"; break;
}
}
}
int main()
{
char A[101],B[101]; //数组要开成101,100位的数进位没法放
int C[101],D[101];
int i,carry;
while(gets(A))
{
gets(B);
for(i=strlen(A);i<101;i++) //把剩下的空间都赋值为0,不然计算时会出错
A[i]=0;
for(i=strlen(B);i<101;i++)
B[i]=0;
if(strlen(A)<strlen(B)) //A为较长的数
sw(A,B,strlen(B));//若B长则交换
fun(A,strlen(A));//倒置
fun(B,strlen(B));
for(i=0;i<strlen(A);i++)
C[i]=0;
for(i=0;i<strlen(A);i++)
D[i]=0;
CTN(A,C,strlen(A));//转化为数字计算
CTN(B,D,strlen(B));
carry=0;
for(i=0;i<strlen(A);i++)//按位计算
{
if(C[i]+D[i]+carry<=19)
{
C[i]=C[i]+D[i]+carry;
carry=0;
}
else
{
C[i]=C[i]+D[i]+carry-20;
carry=1;
}
}
int t0,n;
n=strlen(A);
for(i=0;i<n/2;i++)//再倒置回来
{
t0=C[i];
C[i]=C[n-1-i];
C[n-1-i]=t0;
}
if(carry==1)//最高位若有进位则先输出最高位1
{
cout<<"1";
outp(C,strlen(A));
}
else
outp(C,strlen(A));
cout<<endl;
}
return 0;
}