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

高精度整数加法

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

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

int main() {
    string res;
    string s1;
    string s2;
    getline(cin, s1);
    getline(cin, s2);
    int carry = 0;
    int i = s1.size() - 1;
    int j = s2.size() - 1;
    while (i >= 0 || j >= 0 || carry) {
        int sum = carry;
        if (i >= 0) sum += s1[i--] - '0';
        if (j >= 0) sum += s2[j--] - '0';
        carry = sum / 10;
        sum %= 10;
        res.insert(res.begin(), sum + '0');
    }
    cout << res << endl;
    return 0;
}


全部评论

相关推荐

评论
点赞
收藏
分享
牛客网
牛客企业服务