两个大整数相加,大神来看看

itelly itelly 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.



Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.



Sample Input
2
1 2
112233445566778899 998877665544332211


Sample Output
Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
这是hdoj上的题,这是我的代码,在我机子一运行就死了
#include <iostream>

using namespace std;

int main()
{
string s1,s2,s3;
int a;
cin>>a;
for(int i=1;i<=a;i++)
{
cin>>s1>>s2;
int len1=s1.length();
int len2=s2.length();
int index=0,flag=0;
int len11=len1,len22=len2;
while(len1>=0&&len2>=0)
{
int x=s1.at(index++),y=s2.at(index++);
int sum=x+y+flag;
if(sum>9)
{
flag=1;
sum-=10;
}
else
{
flag=0;
}
s3.append((char*)sum);
len1--;
len2--;
}
if(len1==0)
{
for(int i=len1;i<len11;i++)
{
int s=s1.at(i);
s3.append((char*)s);
}

}
if(len2==0)
{
for(int i=len2;i<len22;i++)
{
int s=s2.at(i);
s3.append((char*)s);
}
}

cout<<"case "<<i<<":"<<endl;
cout<<s1<<" + "<<s2<<" = ";
for(int i=s3.length();i>=0;i--)
{
cout<<s3.at(i);
}
cout<<endl<<endl;


}


return 0;
}
...全文
评论 17
收藏 已收藏
分享
返回顶部
itelly
C++ 语言 65,213 成员 250,513 内容