自己实现的大数乘法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_26671711/article/details/50994079

效率不高,不过自然。

模仿了人类进行计算的过程,不同的是从高位计算到低位,最后逐一进位。

#include <iostream>
#include <string>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	string num1;
	string num2;
	cin >> num1 >> num2;
	int result[1000] = { 0 };

	for (int i = 0; i<num1.size(); i++)//计算
	{
		for (int j = i; j<i + num2.size(); j++)
		{
			int n1 = num1[i] - '0';
			int n2 = num2[j-i] - '0';
			result[j] += n1*n2;
		}
	}



	for (int i = (num1.size()) + (num2.size()) - 1; i>0; i--)
	{
		if (result[i]>9)//进位
		{
			result[i - 1] += result[i] / 10;
			result[i] = result[i] % 10;
		}
	}
	for (int i = 0; i<(num1.size()) + (num2.size())-1; i++)//显示
		cout << result[i];
	cout << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_26671711/article/details/50994079
今日推荐