题解 | #高精度整数加法# c++模拟加法过程
高精度整数加法
http://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6
字符串逆序方向模拟加法过程,注意处理边界进位。
#include<bits/stdc++.h> using namespace std; typedef long long LL; inline int ctoi(char c) { return c - '0'; } void add(string &s1, string &s2) { if (s1.size() > s2.size()) return add(s2, s1); vector<int> vec; int tt = 0; for (int i = 0; i < s2.size(); i++) { tt += i < s1.size() ? (ctoi(s1[i]) + ctoi(s2[i])) : ctoi(s2[i]); vec.push_back(tt % 10); tt /= 10; } // handle carray number if(tt) vec.push_back(tt); for (int i = vec.size() - 1; i >= 0; i--) cout << vec[i]; } int main() { string s1, s2; for (; cin >> s1; ) { cin >> s2; reverse(s1.begin(), s1.end()), reverse(s2.begin(), s2.end()); add(s1, s2); cout << endl; } return 0; }