大数乘法
#include <bits/stdc++.h>
using namespace std;
vector<int> multiplication(const vector<int> &a, const vector<int> &b) {
long la = a.size();
long lb = b.size();
vector<int> res(la + lb - 1);
for (int i = 0; i < la; i++) {
for (int j = 0; j < lb; j++) {
res[i + j] += a[i] * b[j];
}
}
int carry = 0;
for (int i = la + lb - 2; i >= 0; i--) {
res[i] += carry;
carry = res[i] / 10;
res[i] %= 10;
}
if (carry) {
res.insert(res.begin(), carry);
}
return res;
}
int main() {
vector<int> a = {9, 9, 9, 9, 9}, b = {9, 9, 9, 9, 9};
auto res = multiplication(a, b);
for (auto e:res)
cout << e;
cout << endl;
return 0;
}
输出:
9999800001