题解 | #a+b#
a+b
https://www.nowcoder.com/practice/4c39c984ea3848b48e111b8e71ec1dd4
对于高精度加法,很多人其实都了解原理,但是很害怕写,其实高精度加法非常的简单。大家可以参考我这种高精度写法,代码长度都还适中,了解原理之后写这种题跟切菜一样容易。
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string a, b;
while (cin >> a >> b) {
string c;
int n = a.length() - 1, m = b.length() - 1, carry = 0;
while (n >= 0 || m >= 0 || carry) {
if (n >= 0) carry += a[n--] - '0';
if (m >= 0) carry += b[m--] - '0';
c += carry % 10 + '0';
carry /= 10;
}
reverse(c.begin(), c.end());
cout << c << endl;
}
}
