大数相加:A+B problem

#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
using namespace std;
int x[1002]={0},y[1002]={0},sum[1002]={0};
int main(){
	int T;
	cin>>T;
	for(int k=1;k<=T;k++){
		char a[1002],b[10002];
		for(int i=0;i<=1002;i++){
			sum[i]=0;x[i]=0;y[i]=0;
		}
		int maxx;
		cin>>a>>b;
		int l1=strlen(a),l2=strlen(b);
		maxx=max(l1,l2);
		for(int i=maxx-l1+1,j=0;i<=maxx;i++,j++)x[i]=a[j]-48;
		/*for(int i=0;i<=maxx;i++)cout<<x[i];
		cout<<endl;*/
		for(int i=maxx-l2+1,j=0;i<=maxx;i++,j++)y[i]=b[j]-48;
		/*for(int i=0;i<=maxx;i++)cout<<y[i];
		cout<<endl;*/
		for(int i=maxx;i>=1;i--){
			int t=(x[i]+y[i]+sum[i])%10;
			int s=(x[i]+y[i]+sum[i])/10;
			sum[i]=t;
			sum[i-1]+=s;
			
		}
		cout<<"Case "<<k<<":"<<endl<<a<<" + "<<b<<" = ";
		if(sum[0]!=0)cout<<sum[0];
		for(int i=1;i<=maxx;i++)cout<<sum[i];
		if(k==T)cout<<endl;
		else cout<<endl<<endl;	
	}
	return 0;
}

上一篇:AtCoder-abc209 Problem E


下一篇:UVA524素数环 Prime Ring Problem