问题链接:Problem-1002
我的答案:
#include<iostream>
#include<string>
using namespace std;
string AplusB(string a,string b);
int main()
{
int count;
cin>>count;
int i=1;
while (count--)
{
string a,b;
cin>>a>>b;
cout<<"Case "<<i++<<":"<<endl<<a<<"+"<<b<<"="<<AplusB(a,b);
if(count)
cout<<endl<<endl;
/*else
cout<<endl;*/
}
return 0;
}
string AplusB(string a,string b)
{
int decadeNum=0;
int unitNum=0;
int pA=a.length()-1;
int pB=b.length()-1;
string retStr="";
int num1,num2;
while ((pA>=0)&&(pB>=0))
{
num1=a[pA--]-48;
num2=b[pB--]-48;
int tmpNum=num1+num2+decadeNum;
decadeNum=tmpNum/10;
unitNum=tmpNum%10;
retStr=(char)(unitNum+48)+retStr;
}
num1=0;
while ((pA>=0)||(pB>=0))
{
if(pA<0)
{
num2=b[pB--]-48;
}
else
{
num2=a[pA--]-48;
}
decadeNum=(num1+num2+decadeNum)/10;
unitNum=(num1+num2+decadeNum)%10;
retStr=(char)(unitNum+48)+retStr;
}
return retStr;
}
测试通过的代码:
(待续)