高精度模板题,写一下终身受益!
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
class BigNum{
private:
string num;
public:
void input(string s)
{
num = s;
}
string add(string a)
{
string ans = "";
int len = max(a.length(),num.length());
while(a.length()<len+1) a = "0"+a;
while(num.length()<len+1) num = "0"+num;
for(int i=0;i<len+1;i++) ans += "0";
int cf = 0,tf;
for(int i=len;i>=0;i--)
{
tf = a[i]-'0' + num[i]-'0' + cf;
if(tf>=10)
{
cf = 1;
tf-=10;
ans[i] = tf +'0';
}
else
{
cf = 0;
ans[i] = tf + '0';
}
}
if(ans[0]=='0') ans.erase(0,1);
return ans;
}
};
int main()
{
BigNum bn;
string s1,s2;
int T,cnt=1;
cin >> T ;
while(T--)
{
cin >> s1 >> s2;
bn.input(s1);
printf("Case %d:\n",cnt++);
cout << s1 <<" + " << s2 <<" = " <<bn.add(s2) << endl;
if(T) cout << endl;
}
return 0;
}