杭电1002 A+B Problem Ⅱ 简单易懂求解法
来源:互联网 发布:crm源码怎么安装 编辑:程序博客网 时间:2024/06/06 03:03
写完这道题(也经历了几次WA),分享一下思路和解题过程中遇到的坑,以及一些大家WA时可以用得到的测试数据。
个人觉得自己的解法还是非常容易看懂的,其实就是简单的模拟了笔算的算术加法。
先贴题目:
A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 342320 Accepted Submission(s): 66379
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
21 2112233445566778899 998877665544332211
Sample Output
Case 1:1 + 2 = 3Case 2:112233445566778899 + 998877665544332211 = 1111111111111111110
Author
Ignatius.L
简单的翻译一下,其实就是个大数加法的题目。这里的大数呢,可能会有1000位之长。所以第一点:用long之类的是不行的,妥妥的WA
简单的做法是转换成长长的字符串来进行计算。那么大数相加,模拟加法,当然就是个位相加,如果大于十,进一,十位相加,以此类推...
下面贴代码,附注释。
#include <iostream>#include <string>#include <stack>using namespace std;int main(){int n; //case个数int theCase=0; //当前的case,这里设一个变量一是便于输出,二是便于输出格式int count=0; //每个对应位相加得到的数字char a[1005],b[1005]; //字符串表示两个加数int flag=0; //是否需要进一的标志cin>>n;while(n--) //每个case{theCase++;if(theCase!=1) //输出格式cout<<endl;cin>>a>>b;cout<<"Case "<<theCase<<":"<<endl;cout<<a<<" + "<<b<<" = ";int la=strlen(a),lb=strlen(b); //得到两个加数各有几位stack<int> res; //利用栈来输出最终得到的结果,个人认为比较方便if(la>=lb){int k=la-1; //这里比如a=7890的话,la=4,但是计算从a[3]算起,因为下标从0开始嘛flag=0; //初始化为不需要进1for(int i=lb-1;i>-1;i--){count=a[k]-'0'+b[i]-'0'+flag;//char到数字的转换,如果没有这个-'0'会出现错误。flag加上。if(count>=10) //为下一位计算判断是否需要进1flag=1;elseflag=0;res.push(count%10); //把得到的这一位余十存入栈里k--;}for(;k>-1;k--) //较小的b已经加完。下面就是搬运a,判断进位的问题。{count=a[k]-'0'+flag;if(count>=10)flag=1;elseflag=0;res.push(count%10);}if(flag==1) //如果没有这个,99+1会很尴尬res.push(1);while(!res.empty()) //利用栈后进先出的特性,可以很方便的输出{cout<<res.top();res.pop();}cout<<endl;}if(lb>la) //同上{int k=lb-1;flag=0;for(int i=la-1;i>-1;i--){count=b[k]-'0'+a[i]-'0'+flag;if(count>=10)flag=1;elseflag=0;res.push(count%10);k--;}for(;k>-1;k--){count=b[k]-'0'+flag;if(count>=10)flag=1;elseflag=0;res.push(count%10);}if(flag==1)res.push(1);while(!res.empty()){cout<<res.top();res.pop();}cout<<endl;}}}
这段代码是可以AC的。
如果你觉得自己代码写对了,也通过了样例,但是还是WA。可以测试一下以下几组特殊数据看看输出有没有错误。
99+1?
0+0?
7890+123?
如有疏漏,恭请指正。
0 0
- 杭电1002 A+B Problem Ⅱ 简单易懂求解法
- 杭电1002:A+B Problem Ⅱ
- 杭电 1002[A + B Problem II]
- 杭电1002 A + B Problem II
- 杭电 1002:A + B Problem II
- 杭电 1002 A + B Problem II
- 杭电1002 A+B problem II
- 杭电1002 A + B Problem II
- 杭电1002 A + B Problem II
- 杭电1002---A + B Problem II
- 杭电1002 A+B Problem(II)
- 杭电1002 A + B Problem II
- 杭电 1002 A + B Problem II
- 【杭电】[1002]A + B Problem II
- 杭电1002:A + B Problem II
- 杭电-1002 A + B Problem II
- 【杭电1002】A + B Problem II
- 【杭电1002】A + B Problem II
- codevs天梯 fibonacci数列水题
- 史上最全最强SpringMVC详细示例实战教程(图文)
- Linux命令行与Shell脚本编程
- flex4 spark 布局
- 关于ActiveMQ的简单整理(2)
- 杭电1002 A+B Problem Ⅱ 简单易懂求解法
- 【长篇高能】ReactiveCocoa 和 MVVM 入门
- codevs天梯 递归第一次 水题
- 敏捷开发流程
- ORA-12012: error on auto execute of job “SYS”.”BSLN_MAINTAIN_STATS_JOB”
- codevs天梯 3n+1问题 暴力水题
- 1038. Recover the Smallest Number (30)
- Educational Codeforces Round 13 D. Iterated Linear Function 逆元+公式+费马小定理
- 稀疏点云的重建研究