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

全部评论

相关推荐

06-26 15:33
青岛工学院 Java
积极的秋田犬要冲国企:他现在邀请我明天面试
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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