杭电1002-模板2-用string

#include <iostream>
#include <cstring>
using namespace std;
string Add(string a,string b)
{
	int i,j,sum=0;
	if(a.length()<b.length())swap(a,b);
	for(i=a.length()-1,j=b.length()-1;i>=0;i--,j--)
	{
		sum+=a[i]-'0';
		if(j>=0)sum+=b[j]-'0';
		a[i]=sum%10+'0';
		sum/=10;
	}
	if(sum)a='1'+a;
	return a;
}
int main()
{
	int T,Case=0;
	string s1,s2;
	cin>>T;
	while(T--)
	{
		cin>>s1>>s2;
		cout<<"Case "<<++Case<<":"<<endl;
		cout<<s1<<" + "<<s2<<" = ";
		s1=Add(s1,s2);
		cout<<s1<<endl;
		if(T!=0)cout<<endl;
	}
return 0;
}

你可能感兴趣的:(高精度加法)