[ACM]A+B Problem(大数加算3つの方法)
Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
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.
Sample Input
Sample Output
解題の構想:2つの数の各ビットを1つの配列に格納し、逆順序で格納し、対応するビットが加算され、別の配列に格納します.
方法1:
方法2:
方法3:
スクリーンショットの実行:
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
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.
Sample Input
2
1 2
112233445566778899 998877665544332211
Sample Output
Case 1:
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
解題の構想:2つの数の各ビットを1つの配列に格納し、逆順序で格納し、対応するビットが加算され、別の配列に格納します.
方法1:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char x[1001],y[1001];// , x , y
int xx[1001]={0},yy[1001]={0},sum[1001]={0},i,j=1;//xx ( ) x , ,sum
int lenx,leny,maxlen,t;
cin>>t;
while(t--)
{
cin>>x>>y;
lenx=strlen(x);//x
leny=strlen(y);//y
maxlen=lenx>leny?lenx:leny;// maxlen
for(i=0;i<lenx;i++)
xx[i]=x[lenx-i-1]-'0';// ascii '0' 48 '4' 52 , 4 , xx , x[lenx-i-1], ,
for(i=0;i<leny;i++)
yy[i]=y[leny-i-1]-'0';
for(i=0;i<maxlen;i++)
{
sum[i]+=xx[i]+yy[i];//
sum[i+1]=sum[i]/10;// 18, sum[i] 10, sum[i+1] 1, 1
sum[i]=sum[i]%10;// 10, , -10;
}
if(sum[i]==1)
maxlen++;// i , , 10 , 1, s[i] , maxlen+1
cout<<"Case "<<j<<":"<<endl;
j++;
cout<<x<<" + "<<y<<" = ";
for(i=maxlen-1;i>=0;i--)
cout<<sum[i];
cout<<endl;
if(t!=0)
cout<<endl;// ,AC ,
for(i=0;i<maxlen;i++)
{
sum[i]=0;
xx[i]=0;
yy[i]=0;
}// ,
}
}
方法2:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char x[1001],y[10001];
int xx[1001]={0},yy[1001]={0},sum[1001]={0},i,j=1,lenx,leny,maxlen;
int t;
cin>>t;
while(t--)
{
cin>>x>>y;
lenx=strlen(x);
leny=strlen(y);
maxlen=lenx>leny?lenx:leny;
for(i=0;i<lenx;i++)
xx[i]=x[lenx-i-1]-'0';
for(i=0;i<leny;i++)
yy[i]=y[leny-i-1]-'0';
for(i=0;i<maxlen;i++)
{
sum[i]+=xx[i]+yy[i];
sum[i+1]=sum[i]/10;
sum[i]=sum[i]%10;// 1
}
if(sum[i]==1)
maxlen++;
cout<<"Case "<<j<<":"<<endl;
j++;
cout<<x<<" + "<<y<<" = ";
for(i=maxlen-1;i>=0;i--)
cout<<sum[i];
cout<<endl;
if(t!=0)
cout<<endl;
memset(xx,0,1001*sizeof(int));//, xx 0,memset , memset(xx,0,1001); , , 1001*4, 1001*sizeof(int)
memset(yy,0,1001*sizeof(int));
memset(sum,0,1001*sizeof(int));
}
}
方法3:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char x[1001],y[10001];
int xx[1001]={0},yy[1001]={0},sum[1001]={0},i,j=1,lenx,leny,maxlen;
int t;
cin>>t;
while(t--)
{
cin>>x>>y;
lenx=strlen(x);
leny=strlen(y);
maxlen=lenx>leny?lenx:leny;
for(i=0;i<lenx;i++)
xx[i]=x[lenx-i-1]-'0';
for(i=0;i<leny;i++)
yy[i]=y[leny-i-1]-'0';
for(i=0;i<maxlen;i++)
{
sum[i]+=xx[i]+yy[i];
if(sum[i]>=10)// 10
{
sum[i+1]=1;// 10, 1
sum[i]-=10;// 20, -10
}
}
if(sum[i]==1)
maxlen++;
cout<<"Case "<<j<<":"<<endl;
j++;
cout<<x<<" + "<<y<<" = ";
for(i=maxlen-1;i>=0;i--)
cout<<sum[i];
cout<<endl;
if(t!=0)
cout<<endl;
memset(xx,0,1001*sizeof(int));
memset(yy,0,1001*sizeof(int));
memset(sum,0,1001*sizeof(int));
}
}
スクリーンショットの実行: