题目来源
题意简述
[INPUT]
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
[OUTPUT]
For each test case, you should output two lines. The first line is “Case #:”, # means the number of the test case. The second line is the an equation “A + B = Sum”, Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
输入一个数T(1<=T<=20)表示有T个测试用例,接下来T行每行输入a和b两个数。输入的a和b均为正整数,但数字的长度不超过1000。给出a+b的和。
要点分析
本质就是大数加法的运用
严格的格式要求
1.输入为一次输入,输出也为一次输出。
2.每个输出结果之间有一行空行。
3.最后一个输出结果后有且仅有一个空行。
参考代码
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
string a,b;
vector<string> res[3];
int t,r;
cin>>t;
while(t--)
{
cin>>a>>b;
res[0].push_back(a);
res[1].push_back(b);
if(a.size()<b.size())
{
r=b.size()-a.size();
for(int i=a.size()-1;i>=0;i--)
{
b[r+i]+=a[i]-'0';
if(b[r+i]>'9')
{
b[r+i]-=10;
b[r+i-1]++;
}
}
for(int i=a.size()-1;i>=0;i--)
{
if(b[i]>'9')
{
b[i]-=10;
b[i-1]++;
}
}
if(b[0]>'9')
{
b[0]-=10;
b.insert(0,1,'1');
}
res[2].push_back(b);
}
else
{
r=a.size()-b.size();
for(int i=b.size()-1;i>=0;i--)
{
a[r+i]+=b[i]-'0';
if(a[r+i]>'9' && r+i!=0)
{
a[r+i]-=10;
a[r+i-1]++;
}
}
for(int i=b.size()-1;i>=0;i--)
{
if(a[i]>'9' && i!=0)
{
a[i]-=10;
a[i-1]++;
}
}
if(a[0]>'9')
{
a[0]-=10;
a.insert(0,1,'1');
}
res[2].push_back(a);
}
}
for(int i=0;i<res[0].size();i++)
{
if(i!=0)cout<<endl;
cout<<"Case "<<i+1<<":"<<endl;
cout<<res[0][i]<<" + "<<res[1][i]<<" = "<<res[2][i]<<endl;
}
return 0;
}
20200702