题解 | #高精度整数加法#

高精度整数加法

https://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
    string a;
    string b;
    cin >> a;
    cin >> b;
    int i = a.length() - 1;
    int j = b.length() - 1;
    vector<int> res;
    int carry = 0;
    char up;
    char down;
    while(i >= 0 || j >= 0) {
        if(i >= 0) 
            up = a[i];
        else 
            up = '0';
        if(j >= 0)
            down = b[j];
        else 
            down = '0';
        int t = up + down - '0' - '0' + carry;

        res.push_back(t % 10);
        carry = t / 10;
        i--;
        j--;
    }
    if(carry) res.push_back(carry);
    reverse(res.begin(), res.end());
    for(auto& i: res)
        cout << i;
    cout << endl;
    return 0;
}
// 64 位输出请用 printf("%lld")

竖式加法模拟即可。

全部评论

相关推荐

09-17 17:09
门头沟学院 Java
雨忄:有人给出过解法,拖晚点去,然后到时候再找其他理由商量,既增加他们的筛人成本,不一定会给你收回offer ,也能占位避免工贼
秋招的嫡长offer
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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