杭电ACM大数加法题目

ruanjianchjian ruanjianchjian 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 124156 Accepted Submission(s): 23887


Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.



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


Author
Ignatius.L
我写的代码:请各位帮忙看一下为什么是Wrong Answer……

#include<iostream>
#include<string>
using namespace std;
#define Max 1000
int num1[Max];
int num2[Max];

struct bigadd
{
string str1;
string str2;
int * result;
int len;
};

int main()
{
string str1,str2;
int i;
bool flag=false;
int casenumber,len1,len2,len,hold=0;
bigadd * arr;
memset(num1,0,sizeof(num1));
memset(num2,0,sizeof(num2));
cin>>casenumber;
arr = new bigadd[casenumber];
for(i=0;i<casenumber;i++)
{
cin>>str1>>str2;
arr[i].str1=str1;
arr[i].str2=str2;
len1=str1.length();
len2=str2.length();
for(int k=len1-1;k>=0;k--)
{
num1[len1-1-k]=str1[k]-'0';
}
for(int j=len2-1;j>=0;j--)
{
num2[len2-1-j]=str2[j]-'0';
}
len=len1>len2?len1:len2;
arr[i].len=len;
arr[i].result=new int[len+1];
for(int k=0;k<=len;k++)
{
num1[k]+=num2[k]+hold;
if(num1[k]>=10)
{
hold=num1[k]/10;
num1[k]=num1[k]%10;
}
else
hold=0;
}
for(int l=0;l<=len;l++)
{
arr[i].result[l]=num1[l];
}
memset(num1,0,sizeof(num1));
memset(num2,0,sizeof(num2));
}
for( i=0;i<casenumber;i++)
{
cout<<"Case"<<" "<<i+1<<":"<<endl;
cout<<arr[i].str1<<" "<<"+"<<" "<<arr[i].str2<<" "<<"="<<" ";
for(int j=arr[i].len;j>=0;j--)
{
if(arr[i].result[j]||flag)
{
flag=true;
cout<<arr[i].result[j];
}

}
cout<<endl<<endl;
}
}
...全文

相关推荐

评论 6
收藏 已收藏
分享
返回顶部