#include <iostream>
#include <memory>
using namespace std;
// 返回位数size1 + size2
int* multi(int *num1, int size1, int *num2, int size2) {
int size = size1 + size2;
int *ret = new int[size];
int i = 0;
memset(ret, 0, sizeof(int) * size);
for(i = 0; i < size2; ++i) {
int k = i;
for(int j = 0; j < size1; ++j) {
ret[k++] += num2[i] * num1[j];
}
}
for(i = 0; i < size; ++i) {
if(ret[i] >= 10) {
ret[i+1] += ret[i] / 10;
ret[i] %= 10;
}
}
return ret;
}
int main() {
int num1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 1, 1, 1, 1 };
int num2[] = { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5 };
int size1 = sizeof(num1) / sizeof(int);
int size2 = sizeof(num2) / sizeof(int);
cout << "size1: " << size1 << endl;
cout << "size2: " << size2 << endl;
int *ret = multi(num1, size1, num2, size2);
for(int i = size1 + size2 - 1; i >= 0; i--) {
cout << ret[i];
}
cout << endl;
delete[] ret;
return 0;
}
摘自左飞《CPP数据结构原理与经典问题求解》