www.acwing.com/problem/con…

The main idea is to store data as a string and then convert it to an array for decimal addition.

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

void add(vector<int> &num1, vector<int> &num2, vector<int> &res) {
    if(num2.size() > num1.size()) return add(num2, num1, res);
    
    int t = 0;
    for(int i = 0; i < num1.size(a); ++i) { t += num1[i];if(i < num2.size()) t += num2[i];
        res.push_back(t % 10);
        t /= 10;
    }
    
    if(t) res.push_back(t);
}

int main(a) {
    string s1, s2;
    vector<int> num1, num2, res;
    
    cin >> s1 >> s2;
    
    for(int i = s1.size() - 1; i >= 0; --i) num1.push_back(s1[i] - '0');
    for(int i = s2.size() - 1; i >= 0; --i) num2.push_back(s2[i] - '0');
    
    add(num1, num2, res);
    
    for(int i = res.size() - 1; i >= 0; --i) cout << res[i];
    cout << endl;
    
    return 0;
}
Copy the code