高精度乘法模板(大 * 大)
#include <bits/stdc++.h>
using namespace std;
vector<int> mul(vector<int> &A, vector<int> &B){
vector<int> C(A.size() + B.size() + 7, 0); //初始化为0,C.size()可以大一些
for(int i = 0; i < A.size(); i++){ //i = C.size() - 1时,t一定小于0
for(int j = 0; j < B.size(); j++){
C[i + j] += A[i] * B[j];
}
}
int t = 0;
for(int i = 0; i < C.size(); i++){
t += C[i];
C[i] = t % 10;
t /= 10;
}
//C.back() = 0时,要去除前导0(为了保证结果为0时输出0,所以C.size() > 1)
while(C.size() > 1 && C.back() == 0) C.pop_back(); //必须去除前导0,因为最高位很可能为0
return C;
}
int main(){
string a, b;
cin >> a >> b; // a = "1222432323" b = "232343442334"
vector<int> A, B;
for(int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
for(int i = b.size() - 1; i >= 0; i--) B.push_back(b[i] - '0');
vector<int> C = mul(A, B);
for(int i = C.size() - 1; i >= 0; i--) cout << C[i];
return 0;
}
标签:高精度,int,++,back,--,vector,乘法,模板,size
From: https://www.cnblogs.com/csai-H/p/16934483.html