题目描述
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.
输入
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.
输出
For each pair of numbers, write the sum of the 2 numbers in a single line.
样例输入
1234567890
abcdefghij
99999jjjjj
9999900001
样例输出
bdfi02467j
iiiij00000
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{
char s1[101],s2[101];
int k1[101],k2[101];
while(cin>>s1>>s2)
{
memset(k1,0,sizeof(k1));
memset(k2,0,sizeof(k2));
int m=strlen(s1);
int n=strlen(s2);
if(m>n)
{
int p=m-n;
for(int i=m-1; i>=p; i--)
s2[i]=s2[i-p];
for(int i=0; i<p; i++)
s2[i]='0';
}
if(m<n)
{
int p=n-m;
for(int i=n-1; i>=p; i--)
{
s1[i]=s1[i-p];
}
for(int i=0; i<p; i++)
s1[i]='0';
}
if(m<n)
m=n;
// for(int i=0; i<m; i++)
// cout<<s2[i]<<" ";
for(int i=0; i<m; i++)
{
if(s1[i]>'9')
{
s1[i]=s1[i]-'0';
k1[i]=s1[i]-'0'+9;
}
else
k1[i]=s1[i]-'0';
}
for(int i=0; i<m; i++)
{
if(s2[i]>'9')
{
s2[i]=s2[i]-'0';
k2[i]=s2[i]-'0'+9;
}
else
k2[i]=s2[i]-'0';
}
// for(int i=0; i<m; i++)
// cout<<k1[i]<<" ";
int l[1001];
for(int i=m-1; i>=0; i--)
{
if(k1[i]+k2[i]>19)
{
l[i]=k1[i]+k2[i]-20;
k1[i-1]++;
continue;
}
else
l[i]=k1[i]+k2[i];
}
if(k1[0]+k2[0]>19)
cout<<"1";
for(int i=0; i<m; i++)
{
if(l[i]<=9)
cout<<l[i];
else
{
if(l[i]==10)
cout<<"a";
if(l[i]==11)
cout<<"b";
if(l[i]==12)
cout<<"c";
if(l[i]==13)
cout<<"d";
if(l[i]==14)
cout<<"e";
if(l[i]==15)
cout<<"f";
if(l[i]==16)
cout<<"g";
if(l[i]==17)
cout<<"h";
if(l[i]==18)
cout<<"i";
if(l[i]==19)
cout<<"j";
}
}
cout<<endl;
memset(s1,0,sizeof(s1));
memset(s2,0,sizeof(s2));
}
return 0;
}

本文介绍了一个有趣的编程挑战,源自火星数学竞赛的背景设定,参赛者需编写程序来计算两个20进制的大数相加。火星居民使用20进制数系统,这要求程序能够正确处理从0到19的数字和字母a到j的表示。通过解析示例输入和输出,展示了如何将字符转换为数值进行计算,并在结果中将数值转换回20进制的表示形式。
456

被折叠的 条评论
为什么被折叠?



