题目:
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
解题思路:
用字符串存放输入的数字,将字符串反序,从最小位开始相加,用s函数将字符转换为数字,每位两个数字想加存入r数组,对于
对于r的每位数若大于19就进位,然后在将数组r用q函数转换为字符输出
细节处理:
注意两个数字长度可能不同,比如处理时一个数字每位都加了,而另一个数字还未加完的情况,还有就是最后输出也是反序输出
代码:
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
string a,b;
int r[105];
int s(char d)
{
if(d>='0'&&d<='9')
return d-'0';
else if(d>='a'&&d<='j')
return d-'a'+10;
}
char q(int i)
{
if(i>=0&&i<=9)
return '0'+i;
else if(i>=10&&i<=19)
return 'a'+i-10;
}
int main()
{
int len1,len2,len3;
int i,j;
char c;
while(cin>>a>>b)
{
len1=a.length();
len2=b.length();
memset(r,0,sizeof(r));
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
for(i=0;i<min(len1,len2);i++)
{
r[i]=s(a[i])+s(b[i]);
}
for(i=min(len1,len2);i<max(len1,len2);i++)
{
if(len1>len2)
r[i]=s(a[i]);
else
r[i]=s(b[i]);
}
len3=i;
for(i=0;i<len3;i++)
{
if(r[i]>=20)
{
r[i+1]++;
r[i]-=20;
}
}
if(r[len3]!=0)
len3++;
for(i=len3-1;i>=0;i--)
cout<<q(r[i]);
cout<<endl;
}
return 0;
}
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
解题思路:
用字符串存放输入的数字,将字符串反序,从最小位开始相加,用s函数将字符转换为数字,每位两个数字想加存入r数组,对于
对于r的每位数若大于19就进位,然后在将数组r用q函数转换为字符输出
细节处理:
注意两个数字长度可能不同,比如处理时一个数字每位都加了,而另一个数字还未加完的情况,还有就是最后输出也是反序输出
代码:
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
string a,b;
int r[105];
int s(char d)
{
if(d>='0'&&d<='9')
return d-'0';
else if(d>='a'&&d<='j')
return d-'a'+10;
}
char q(int i)
{
if(i>=0&&i<=9)
return '0'+i;
else if(i>=10&&i<=19)
return 'a'+i-10;
}
int main()
{
int len1,len2,len3;
int i,j;
char c;
while(cin>>a>>b)
{
len1=a.length();
len2=b.length();
memset(r,0,sizeof(r));
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
for(i=0;i<min(len1,len2);i++)
{
r[i]=s(a[i])+s(b[i]);
}
for(i=min(len1,len2);i<max(len1,len2);i++)
{
if(len1>len2)
r[i]=s(a[i]);
else
r[i]=s(b[i]);
}
len3=i;
for(i=0;i<len3;i++)
{
if(r[i]>=20)
{
r[i+1]++;
r[i]-=20;
}
}
if(r[len3]!=0)
len3++;
for(i=len3-1;i>=0;i--)
cout<<q(r[i]);
cout<<endl;
}
return 0;
}