华为机试:高精度整数加法

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

int main() {
    string str;
    string str1;
    cin >> str >> str1;
    reverse(str.begin(), str.end());
    reverse(str1.begin(), str1.end());
    int i = 0, j = 0;
    int n = str.length(), m = str1.length();
    string str2;
    int d = 0;
    while (i < n && j < m) {
        int a = str[i] - '0';
        int b = str1[j] - '0';
        int c = a + b + d;
        if (c > 9) {
            d = c / 10;
            c = c % 10;
        }
        else {
            d = 0;
        }
        str2.push_back(c + '0');
        i++, j++;
    }
    while (i < n) {
        int a = str[i] - '0';
        int c = a + d;
        if (c > 9) {
            d = c / 10;
            c = c % 10;
        }
        else {
            d = 0;
        }
        str2.push_back(c + '0');
        i++;
    }
    while (j < m) {
        int a = str1[j] - '0';
        int c = a + d;
        if (c > 9) {
            d = c / 10;
            c = c % 10;
        }
        else {
            d = 0;
        }
        str2.push_back(c + '0');
        j++;
    }
    if (d != 0) {
        str2.push_back(d + '0');
    }
    reverse(str2.begin(), str2.end());
    cout << str2 << endl;
    return 0;
}

全部评论
没有解题思路吗?谢谢
点赞 回复 分享
发布于 2023-03-08 13:36 天津
这个干货啊,马克一个
点赞 回复 分享
发布于 2023-03-08 13:27 四川

相关推荐

04-13 18:10
门头沟学院 Java
想熬夜的小飞象在秋招:被腾讯挂了后爸妈以为我失联了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务