杭电ACM大数加法题目

ruanjianchjian 2012-09-23 06:38:16
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;
}
}
...全文
342 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ruralboy1 2012-10-04
  • 打赏
  • 举报
回复
用字符数组处理,这题貌似是杭电1002还是1003的吧
litiouslove 2012-10-02
  • 打赏
  • 举报
回复
3
123 1
4 2
11 9
Case 1:
123 + 1 = 124

Case 2:
4 + 2 = 06

Case 3:
11 + 9 = 020

这组数据...首位多了0
litiouslove 2012-10-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

这组数据:
2
11 11
1 1
你flag没有对于每个数据初始化false。

另外
Output a blank line between two test cases.
可能最后一条后面不应该输出endl。
[/Quote]
最后一行是要输出endl的,中间是要输出两个endl的。
需要输出一个空行嘛,自然得两个endl,另外杭电的题貌似最后都得endl。
楼主也在搞ACM吗?我刚刚入门,小菜一个,一起努力呗?
skillart 2012-10-02
  • 打赏
  • 举报
回复
以前用c的字符串模拟,后来又用java的大数累,真的吊爆了。
LogicTeamLeader 2012-09-30
  • 打赏
  • 举报
回复
难道不能用java中的BigInteger么?

[Quote=引用楼主 的回复:]
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 si……
[/Quote]
FancyMouse 2012-09-25
  • 打赏
  • 举报
回复
这组数据:
2
11 11
1 1
你flag没有对于每个数据初始化false。

另外
Output a blank line between two test cases.
可能最后一条后面不应该输出endl。

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧