大整数乘法问题

#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数据结构原理与经典问题求解》

    原文作者:大整数乘法问题
    原文地址: https://blog.csdn.net/yixiu868/article/details/47375403
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞