#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;
}
超大整数乘法模板(高精度乘以低精度)
最新推荐文章于 2023-11-18 21:13:18 发布