杭电ACM大数加法题目
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……
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;
}
}
...全文
相关推荐
杭电ACM1002解题答案
本人在ACM上做题时遇到不少问题,在网上寻求不少答案后终于弄明白。答案已被杭电AC,希望对各位有帮助。。
杭电OJ 1000-1099刷题总结:从入门到算法进阶
在线评测系统(OJ)作为算法学习与编程实践的核心平台,要求开发者不仅掌握语法,更需具备分析问题、设计算法与调试代码的综合能力。从处理多组输入输出到理解递归、贪心、动态规划等经典算法,每一道题目都在强化对计算思维与边界条件的认知。无论是备战ACM、蓝桥杯等竞赛,还是系统夯实数据结构与算法基础,系统化刷题都是高效路径。本文总结杭电OJ 1000-1099题目的刷题经验,按照题型拆解难度梯度,剖析常见错误如Presentation Error、状态残留,并给出C/C++语言选择与查错模板建议,帮助初学者避开弯路,
ACM杭电1002 C++程序
ACM杭电Problem 1002 C++程序 大数相加问题,注意输出的限制
ACM入门十题(杭电oj)
涵盖注册、入门的十题答案,适合刚刚接触hdu的同学。
杭电题目acm答案.doc
杭电题目acm答案.doc


