HDU1002 A + B Problem II

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 426568    Accepted Submission(s): 82846

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

深夜做水题(更新:完全不是水题!!本以为是个青铜没想到是王者!!)。一开始想只用stack,后来发现太麻烦,于是加上了deque。提交的时候又接连出现两次错误,一次Compilation Error,

error C2679: 二进制“>>”: 没有找到接受“std::string”类型的右操作数的运算符(或没有可接受的转换)

查了一下发现是头文件要写成#include<string>而不能是#include<cstring>和#include<string.h>。看来以后写成#include<string>保险点。

还有一次错误是Presentation Error(罪魁祸首就是它!,为了这个错我懵逼了半个多小时!),除了最后一组测试是一个换行,其他都是两个换行,ACM的题还是太讲究了。

①利用栈先进后出(LIFO)的特点做运算

②为了防止sum的第一位进位可以求出sum后加个前导0再处理进位。最后要去掉前导0

测试数据补充

0001 1000

0 0

000 0000

9999 1

1 9999

99900 00999

00999 99900

AC代码:

 1 #include<iostream>
 2 #include<string>
 3 #include<stack>
 4 #include<deque>
 5 using namespace std;
 6 string Sum(string a,string b)
 7 {
 8     stack<int>num1;
 9     stack<int>num2;
10         deque<int>sum;
11     string ans;
12     int i,la=a.length(),lb=b.length();
13     for(i=0;i<la;i++) num1.push(a[i]-'0');
14     for(i=0;i<lb;i++) num2.push(b[i]-'0');
15     while(!num1.empty()||!num2.empty())
16     {
17         if(num1.empty())
18         {
19             sum.push_front(num2.top());
20             num2.pop(); 
21         }
22         else if(num2.empty())
23         {
24             sum.push_front(num1.top());
25             num1.pop();
26         }
27         else 
28         {
29             sum.push_front(num1.top()+num2.top());
30             num1.pop();num2.pop();
31         }
32     }
33     sum.push_front(0);//嵌入一个0 
34         for(i=sum.size()-1;i>=0;i--) //处理进位 
35         if(sum[i]>=10)
36         {
37             sum[i]-=10;
38              sum[i-1]+=1;
39     }
40     if(sum.front()>=10) 
41     {
42         sum.front()-=10;
43         sum.push_front(1);
44         }
45     while(!sum.empty())
46     {
47         ans+=char(sum.front()+'0');
48         sum.pop_front(); 
49     }
50     while(ans.find("0",0)==0&&ans.length()>1) ans=ans.substr(1);//去掉前导0 
51     return ans;
52 }
53 int main()
54 {
55     int T,i=1;
56     cin>>T;
57     string A,B;
58     while(T--)
59     {
60         cin>>A>>B;
61         cout<<"Case "<<i++<<':'<<endl;
62         cout<<A<<" + "<<B<<" = ";//注意空格 
63         cout<<Sum(A,B)<<endl;
64         if(T>0) cout<<endl;//除了最后一组测试是一个换行,其他都是两个换行
65     }
66 }
View Code
原文地址:https://www.cnblogs.com/wangzhebufangqi/p/12796220.html