21 条题解

  • -2
    @ 2022-7-3 20:56:09

    #include <iostream> #include <string> using namespace std; void bigAdd(string num1, string num2, string&res){

    if (num1.size() == 0){
        res = num2;
        return;
    }
    if (num2.size() == 0){
        res = num1;
        return;
    }
    
    res = "";
    int n1 = num1.size()-1, n2 = num2.size()-1;
    int carry = 0;
    while (n1 >= 0 || n2 >= 0){
    
        int a = n1 >= 0 ? num1[n1--] - '0' : 0;
        int b = n2 >= 0 ? num2[n2--] - '0' : 0;
    
        int t = carry + a + b;
        carry = t / 10;
        t = t % 10;
        res = to_string(t) + res;
    }
    //判断是否还有进位
    while (carry){
        int t = carry / 10;
        carry %= 10;
        res = to_string(carry) + res;
        carry = t;
    }
    return;
    

    } //测试函数 int main(){

    string n1, n2, n3;
    while (cin >> n1 >> n2){
    
        bigAdd(n1,n2,n3);
        cout << n3 << endl;
    }
    return 0;
    

    }

    信息

    ID
    6800
    时间
    1000ms
    内存
    128MiB
    难度
    1
    标签
    递交数
    770
    已通过
    295
    上传者