大数不能看成单纯的数,得用字符来替代
#include<iostream> #include<string> using namespace std; string account(string num1,string num2) { if(num1.size()<num2.size()) { string temp=num1; num1=num2; num2=temp; } int length1=num1.size(),length2=num2.size(),flag=0,a,b,sum; while(length1>0) { a=num1[length1-1]-'0'; if(length2>0) b=num2[length2-1]-'0'; else b=0; sum=a+b+flag; if(sum>=10) { num1[length1-1]='0'+sum%10; flag=1; } else { num1[length1-1]='0'+sum; flag=0; } length1--; length2--; } if(1==flag) num1="1"+num1; return num1; } int main() { int t; int x=1,n,m; cin>>t; while(t--) { string a,b; cin>>a>>b; cout<<"Case "<<x<<":"<<endl; cout<<a<<" + "<<b<<" = "<<account(a,b)<<endl; if(t!=0) cout<<endl; x++; } return 0; }