高精度加法
重点
1. 模拟加法运算过程
2. t表示进位,开始进位为0
3. 最后判断进位不为0,则需要加入c中
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 1000010;
vector<int> add(vector<int> &a, vector<int> &b) {
vector<int> c;
int t = 0;
for (int i = 0; i < a.size() || i < b.size(); i++) {
if (i < a.size()) t += a[i];
if (i < b.size()) t += b[i];
c.push_back(t % 10);
t /= 10;
}
if (t > 0) c.push_back(t);
return c;
}
int main() {
string s1, s2;
cin >> s1 >> s2;
vector<int> a, b;
for (int i = s1.size() - 1; i >= 0; i--) a.push_back(s1[i] - '0');
for (int i = s2.size() - 1; i >= 0; i--) b.push_back(s2[i] - '0');
auto c = add(a, b);
for (int i = c.size() - 1; i >= 0; i--) printf("%d", c[i]);
return 0;
}