学习目标:
No.5高精度算法之
高精度乘法
学习内容:
高精度乘法模板:
#include <iostream>
#include <vector>
using namespace std;
vector<int> mul(vector<int> &A, int b) // C = A * b, A >= 0, b >= 0
{
vector<int> C;
int t = 0;
for (int i = 0; i < A.size() || t; i ++ )
{
if (i < A.size()) t += A[i] * b;
C.push_back(t % 10);
t /= 10;
}
//123 * 0 = 000 同样需要去掉前导0
while (C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
int main()
{
string a;
int b;//b很小,直接int定义即可
cin >> a >> b;
vector<int> A;
for(int i = a.size() - 1; i >= 0; i -- ) A.push_back(a[i] - '0');
auto C = mul(A, b);
for(int i = C.size() - 1; i >= 0; i -- ) printf("%d", C[i]);
return 0;
}