1002 A + B Problem II [ACM刷题]
这一段时间一直都在刷OJ,这里建一个博客合集,用以记录和分享算法学习的进程。
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
AC CODE
//这一版AC代码,由于题目对于给定的输入输出太多限定,所以代码有很多冗余的部分,
//核心思想就是使用堆栈的方法,使用三个堆栈实现了大数的加法
#include <iostream> #include <string> #include <stack> #include <vector> using namespace std; int count = 0;//声明一个count全局变量,用作进位计算 int main() { int i, n; vector<string> vec1; vector<string> vec2; string str1, str2; stack<char> stk1; stack<char> stk2; stack<int> rstk; cin >> n; //完成大数字符串的输入 for(i = 0; i < n; ++i) { cin >> str1 >> str2; vec1.push_back(str1); vec2.push_back(str2); } for(i = 0; i < n; ++i) { //将数字以char字符形式读入并push进堆栈中 for(const auto x : vec1[i]) { stk1.push(x); } for(const auto y : vec2[i]) { stk2.push(y); } //取堆栈元素进行加法运算 while(!stk1.empty() && !stk2.empty()) { int temp = stk1.top() - '0' + stk2.top() - '0' + count; if(temp > 9) { temp %= 10; count = 1; } else { count = 0; } rstk.push(temp); stk1.pop(); stk2.pop(); } //进行多余高位的push while(!stk1.empty()) { int temp = stk1.top() - '0' + count; if(temp > 9) { temp %= 10; count = 1; } else { count = 0; } rstk.push(temp); stk1.pop(); } while(!stk2.empty()) { int temp = stk2.top() - '0' + count; if(temp > 9) { temp %= 10; count = 1; } else { count = 0; } rstk.push(temp); stk2.pop(); } while(count == 1) { rstk.push(1); count = 0; } cout << "Case " << i+1 << ":" << endl; cout << vec1[i] << " " << "+" << " " << vec2[i] << " = "; //最终结果打印出来即可 while(!rstk.empty()) { cout << rstk.top(); rstk.pop(); } cout << endl; while(i < n-1) { cout << endl; break; } } return 0; }
精简代码
/*这一版是简化版的大数加法代码 *去除了一部分输入输出规则,以核心代码为主 */ #include <iostream> #include <string> #include <stack> #include <vector> using namespace std; int count = 0;//声明一个count全局变量,用作进位计算 int main() { int i = 0; string str1, str2; stack<char> stk1; stack<char> stk2; stack<int> rstk; while(cin >> str1 >> str2) { //将数字以char字符形式读入并push进堆栈中 for(const auto x : str1) { stk1.push(x); } for(const auto y : str2) { stk2.push(y); } //取堆栈元素进行加法运算 while(!stk1.empty() && !stk2.empty()) { int temp = stk1.top() - '0' + stk2.top() - '0' + count; if(temp > 9) { temp %= 10; count = 1; } else { count = 0; } rstk.push(temp); stk1.pop(); stk2.pop(); } //进行多余高位的push while(!stk1.empty()) { int temp = stk1.top() - '0' + count; if(temp > 9) { temp %= 10; count = 1; } else { count = 0; } rstk.push(temp); stk1.pop(); } while(!stk2.empty()) { int temp = stk2.top() - '0' + count; if(temp > 9) { temp %= 10; count = 1; } else { count = 0; } rstk.push(temp); stk2.pop(); } while(count == 1) { rstk.push(1); count = 0; } cout << "Case " << i+1 << ":" << endl; cout << str1 << " " << "+" << " " << str2 << " = "; //最终结果打印出来即可 while(!rstk.empty()) { cout << rstk.top(); rstk.pop(); } cout << endl << endl; ++i; } return 0; }
PS:网上OJ的输入输出的形式规范实在是太蛋疼了!!!