大数相乘

题意:给定两个大数,求相乘结果

题解:利用数组来存中间结果以及结果。

void mulInteger_2(string &str1, string &str2)   //两个整数相乘
{
	int num1[200] = {0};
	int num2[200] = {0};
	int result[300] = {0};
	//auto_ptr<string> output(new string (str2));

	for(int i = 0; i < str1.size(); i++)
	{
		num1[i] = (int)(str1[str1.size() - 1 - i] - '0');
		
	}
	for(int i = 0; i < str2.size(); i++)
	{
		num2[i] = (int)(str2[str2.size() - 1 - i] - '0');	
	}
	
	for(int i = 0; i < str1.size(); i++)
	{
		for(int j = 0; j < str2.size(); j++)
		{
			result[i+j] += num1[i] * num2[j];
		}
	}

	for(int i = 0; i < 300; i++)
	{
		if(result[i] >= 10)
		{
			result[i+1] += result[i]/10;
			result[i] = result[i]%10;
		}
	}
	
	int i, j, count = 0;
	for( i = 300-1; i >= 0; i--)
		if(result[i]) break;
	//string output;
	char *output = new char[i+2];
	for(j = i; j >= 0; j--)
	{
		output[count] = (char)(result[j] + '0');
		count++;
		//cout << result[j];
	}
	output[count] = '\0';
	//cout << str2[count] << endl
	str2.assign(output);
<span style="white-space:pre">	</span>cout<<output;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值