HDU-1002(简单大数加法) A + B Problem II

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
 
 
分析:高精度大数的运算,直接套用刘汝佳老师的模板。注意坑点:每个case之间都要空一行,且最后一个case后不用空行。
 
 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <ctime>
 5 #include <iostream>
 6 #include <istream>
 7 #include <ostream>
 8 #include <algorithm>
 9 #include <set>
10 #include <vector>
11 #include <sstream>
12 #include <queue>
13 #include <typeinfo>
14 #include <fstream>
15 #include <map>
16 #include <stack>
17 using namespace std;
18 #define INF 100000
19 typedef long long ll;
20 const int maxn=1010;
21 struct BigInteger {
22   static const int BASE = 100000000;
23   static const int WIDTH = 8;
24   vector<int> s;
25 
26   BigInteger(long long num = 0) { *this = num; } 
27   BigInteger operator = (long long num) {
28     s.clear();
29     do {
30       s.push_back(num % BASE);
31       num /= BASE;
32     } while(num > 0);
33     return *this;
34   }
35   BigInteger operator = (const string& str) {
36     s.clear();
37     int x, len = (str.length() - 1) / WIDTH + 1;
38     for(int i = 0; i < len; i++) {
39       int end = str.length() - i*WIDTH;
40       int start = max(0, end - WIDTH);
41       sscanf(str.substr(start, end-start).c_str(), "%d", &x);
42       s.push_back(x);
43     }
44     return *this;
45   }
46   BigInteger operator + (const BigInteger& b) const {
47     BigInteger c;
48     c.s.clear();
49     for(int i = 0, g = 0; ; i++) {
50       if(g == 0 && i >= s.size() && i >= b.s.size()) break;
51       int x = g;
52       if(i < s.size()) x += s[i];
53       if(i < b.s.size()) x += b.s[i];
54       c.s.push_back(x % BASE);
55       g = x / BASE;
56     }
57     return c;
58   }
59 };
60 
61 ostream& operator << (ostream &out, const BigInteger& x) {
62   out << x.s.back();
63   for(int i = x.s.size()-2; i >= 0; i--) {
64     char buf[20];
65     sprintf(buf, "%08d", x.s[i]);
66     for(int j = 0; j < strlen(buf); j++) out << buf[j];
67   }
68   return out;
69 }
70 
71 istream& operator >> (istream &in, BigInteger& x) {
72   string s;
73   if(!(in >> s)) return in;
74   x = s;
75   return in;
76 }
77 set<BigInteger> s;
78 
79 int main()
80 {
81     int t,times=0,tmp;
82     BigInteger a,b;
83     scanf("%d",&t);
84     tmp=t;
85     while(t--){
86         cin>>a>>b;
87         BigInteger sum=a+b;
88         printf("Case %d:
",++times);
89         cout<<a<<" + "<<b<<" = "<<sum<<endl;
90         if(times!=tmp) printf("
");
91     }
92     return 0;
93 }