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

#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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

庞老板

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值