您的位置:首页 > 其它

超大整数乘法模板(高精度乘以低精度)

2012-11-29 18:32 375 查看
#include <cstdio>
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

#define SIZE
#define LENGTH 1000
#define LAST LENGTH-2

char a[LENGTH], result[LENGTH];

//multiplier最大值200,000,000,product的初值为0
char *IntegerMultiplication(char *multiplicand, int multiplier, char *product)
{
	int i, j, first, temp[LENGTH];
	memset(temp, 0, sizeof(temp));
	//将乘积按位保存在temp中,暂不处理进位
	for (i = strlen(multiplicand)-1, j = LAST; i >= 0; i--, j--)
	{
		temp[j] = (multiplicand[i]-'0') * multiplier;
	}
	//获取乘积的首位位置
	first = j - (int)log10(multiplier);
	//处理进位
	for (i = LAST; i >= first; i--)
	{
		product[i] = temp[i] % 10 + '0';
		temp[i-1] += temp[i] / 10;
	}
	//去除前导'0'
	while (product[first] == '0' && first < LAST)
	{
		first++;
	}
	//返回乘积首位地址
	return &product[first];
}

int main(void)
{
	int b;
	while (cin>>a>>b)
	{
		memset(result, 0, sizeof(result));
		cout<<IntegerMultiplication(a, b, result)<<endl;
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: