原题:
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
题意:
输入两个大数求和。
题解:
就是大数加和的问题,用字符串储存,然后相当于竖式加法,注意进位和输出格式的问题就ok了
代码:AC
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
char A[1020];
char B[1020];
char C[1020];
char D[1020];
char E[1020];
int main()
{
int n;
cin>>n;
int i;
for(i=0;i<n;i++)
{
if(i)
cout<<endl;
cin>>A>>B;
int a=strlen(A);
int b=strlen(B);
memset(C,'0',sizeof(C));
memset(D,'0',sizeof(D));
memset(E,'0',sizeof(E));
int j,k=1020;
for(j=0;j<a;j++)
{
C[k-a]=A[j];
k++;
}
k=1020;
for(j=0;j<b;j++)
{
D[k-b]=B[j];
k++;
}
int maxn=max(a,b)+1;
int sum=0;
for(j=1019;j>=1019-maxn-1;j--)
{
E[j]=(C[j]+D[j]-2*'0'+sum)%10+'0';
if((C[j]+D[j]-2*'0'+sum)>=10)
sum=1;
else
sum=0;
}
for(j=0;j<1020;j++)
{
if(E[j]!='0')
break;
}
cout<<"Case "<<i+1<<":"<<endl;
cout<<A<<" + "<<B<<" = ";
for(;j<1020;j++)
cout<<E[j];
cout<<endl;
}
return 0;
}