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:
Sample Output:
题意:
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
输入两个20进制的数,a代表10,b代表11等直到j代表19;输出两数相加之和
分析:
高精度计算,先用string形式输入,然后然后转化成int形式相加,最后通过数组形式输出
代码:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string a,b;
vector<int>x,y,z;
char k;
int i,j,t,p,r;
while(cin>>a>>b)
{
for(i=a.size()-1;i>=0;i--)
{
if(a[i]>='a')
x.push_back(int(a[i]-'a'+10));
else
x.push_back(int(a[i]-'0'));
}
for(i=b.size()-1;i>=0;i--)
{
if(b[i]>='a')
y.push_back(int(b[i]-'a'+10));
else
y.push_back(int(b[i]-'0'));
}
r=max(x.size(),y.size());
for(i=0,t=0;i<r;i++)
{
p=t;
if(i>=x.size())
x.push_back(0);
if(i>=y.size())
y.push_back(0);
t=(x[i]+y[i]+t)/20;
z.push_back((x[i]+y[i]+p)%20);
}
if(t!=0)
cout<<t;
for(j=i-1;j>=0;j--)
{
if(z[j]>9)
{
k=z[j]-10+'a';
cout<<k;
}
else
cout<<z[j];
}
cout<<endl;
x.clear();
y.clear();
z.clear();
}
}
感受:
我用的vector,输入的时候格式错了,检查了好久