HDOJ_1002_A + B Problem II

来源:互联网 发布:最优化理论与方法pdf 编辑:程序博客网 时间:2024/06/06 17:03

题目: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


这个题目的考察点是大数相加,我的思路是:

1.用字符串保存输入的大数。

2.记录字符串的长度,并从最后一位开始相加,每次相加的元素应为 a[k]+b[k]+remainder

如:123+12345,从两数的最后一位(3,5)开始相加。用求余方法(%)得到该位置应该有的数值,并存入最终结果数组的对应位置(sum[i])用取整方法(n/10)得到相加后的进位(用remainder记录)

3.本题中对于最终结果的数组(sum[1200]),我采用倒着输出的方法,即将最后一位相加的结果放在第一个位置(sum[0]),只需要在最后输出结果时从后往前即可。

如:123+12345,最后一位的计算3+5=8,将8存在sum[0].

4.需要考虑两数的长度关系。

    当a.length<b.length时,则将b剩下的元素依次与进位相加并直接存在sum数组中。    当a.length>b.length时,类似。    当a.length=b.length时,查看最高位相加后有没有进位,存进sum数组。

5.消除sum数组中多余的0.(若sum数组中为100,倒叙输出为001,前两个0无用,消去)。

6.将sum数组倒着输出。


该题目易错点(那些年我栽过的坑)

1.该题目为大数相加,数的长度可能会达到1000位,(long long类型的数据范围是-9223372036854775808 ~ +9223372036854775807 )。因此应该选用字符串这种方法,中心思想一定要对。

2.格式问题,每组测试数据之间有一个空行,而不是每个测试数据之后有一个空行。

3.格式问题, case 首字母大写“Case”,以及字符之间的空格问题。

4.在最后一个测试用例的最后是不需要有回车的。

5.先导0问题。输入的时候,数据可能有前导0, 会导致你的结果中多输出0

比如这个case 9999 000000001

6.当输入数据中有0时,直接结束程序,因为题目要求输入数据为正整数。(不清楚这个会不会影响AC,我没有试)

如:case 3    0则跳出程序。

7.在两数长度不相等的时候,计算多余部分时,不能直接将数值直接赋给sum[k],应该计算(a[k]+remainder)%10,也要计算进位,不要忘记。

8.一定要使测试用例尽可能的全面。

大家可以测一下0    00    29999    99999999   00000199  1112345678       123

代码如下:

#include<iostream>#include<string>using namespace std;void output_sum(string a,string b){    int sum[1200];    int al = a.length();//a字符串的长度    int bl = b.length();//b字符串的长度    int an,bn;//a,b字符串中每一位的数字     int remainder = 0;//两数相加的进位     int i;//for循环中计数的量     int minl = (al<=bl)?al:bl;    for(i = 0;i<minl;i++){        an = a[al-i-1]-'0';        bn = b[bl-i-1]-'0';         sum[i] = (an+bn+remainder)%10;        remainder = (an+bn+remainder)/10;//      cout<<"i的值是:"<<i<<" "<<sum[i]<<" "<<remainder<<endl;    }     if(al>bl){        for(;i<al;i++){            sum[i] = (a[al-i-1]-'0'+remainder)%10;            remainder = (a[al-i-1]-'0'+remainder)/10;//          cout<<"i的值是:"<<i<<" "<<sum[i]<<" "<<remainder<<endl;        }        sum[i] = remainder;    }else if(al<bl){        for(;i<bl;i++){            sum[i] = (b[bl-i-1]-'0'+remainder)%10;            remainder = (b[bl-i-1]-'0'+remainder)/10;//          cout<<"i的值是:"<<i<<" "<<sum[i]<<" "<<remainder<<"========="<<endl;        }        sum[i] = remainder;    }    else{        if(remainder!=0){            sum[i] = remainder;//          cout<<"i的值是:"<<i<<" "<<sum[i]<<" "<<remainder<<endl;        }    }    while(sum[i]==0&&i!=0) {        i--;    }    for(;i>=0;i--){        cout<<sum[i];    }    cout<<endl;}int main(){    int t;    int casenum=0;    cin>>t;    string a,b;     while(t--){        cin>>a>>b;        casenum++;        if(a=="0"||b=="0"){            break;        }else{            cout<<"Case "<<casenum<<":"<<endl;            cout<<a<<" + "<<b<<" = ";            output_sum(a,b);            if(t!=0){                cout<<endl;            }        }    }    return 0;}

终于明白一个博主说的血泪史是什么意思了,提交了20几次,各种看前人资料才AC掉,我的妈~,吓得我赶紧写个博客纪念一下。

2017.9.5~~~溜了溜了


感谢https://zhidao.baidu.com/question/469832240.html的回答者,一下子指出了很多问题,我发现自己也有相同的问题,赶紧改,不然现在肯定还在崩溃中,

原创粉丝点击