题解 | #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;
    }
}

全部评论

相关推荐

Yki_:你要算时间成本呀,研究生两三年,博士三四年,加起来就五六年了,如果你本科去腾讯干五年,多领五年的年薪,加上公司内涨薪,可能到时候十五年总薪资也跟博士差不多
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务