AcWing 791. 高精度加法
原题链接
简单
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
vector<int> strToVec(string x) {
vector<int> A;
for (int i = 0; i < x.size(); i ++) A.push_back(x[i] - '0');
reverse(A.begin(), A.end());
return A;
}
vector<int> add(vector<int> A, vector<int> B) {
vector<int> C;
int t = 0;
if (A.size() < B.size()) {
vector<int> T = A;
A = B;
B = T;
}
for (int i = 0; i < A.size(); i ++) {
if (i < B.size()) {
C.push_back((A[i] + B[i] + t) % 10);
t = (A[i] + B[i] + t) / 10;
} else {
C.push_back((A[i] + t) % 10);
t = (A[i] + t) / 10;
}
if (i == A.size() - 1 && t >= 1) {
C.push_back(t);
}
}
reverse(C.begin(), C.end());
return C;
}
int main() {
string a, b;
cin >> a >> b;
vector<int> A = strToVec(a);
// for (int i = 0; i < A.size(); i ++) cout << A[i];
// cout << '\n';
vector<int> B = strToVec(b);
// for (int i = 0; i < B.size(); i ++) cout << B[i];
// cout << '\n';
vector<int> C = add(A, B);
for (int i = 0; i < C.size(); i ++) cout << C[i];
cout << '\n';
return 0;
}