高精度之乘法

题目描述 Description

给出两个正整数A和B,计算A*B的值。保证A和B的位数不超过500位。

输入描述 Input Description

读入两个用空格隔开的正整数

输出描述 Output Description

输出A*B的值

样例输入 Sample Input

3 12

样例输出 Sample Output

36

数据范围及提示 Data Size & Hint

两个正整数的位数不超过500位

#include<stdio.h>
#include<string.h>
int main(){
	char  c[505], d[505];
	int sum[1005], a[505], b[505];
	memset(sum, 0, sizeof(sum));
	/*gets_s(c);
	gets_s(d);*/
	scanf("%s%s", c, d);
	int i, j,k, m, tmp, t,cnt;
	for (i = strlen(c) - 1, k = 0; i >= 0; i--, k++)
		a[k] = c[i] - '0';
	for (i = strlen(d) - 1, m= 0; i >= 0; i--, m++)
		b[m] = d[i] - '0';
	t = 0;
	for (i = 0; i < k; i++){
		cnt = i;
		for (j = 0; j < m; j++){
			tmp = a[i] * b[j] + sum[cnt] + t;
			t = tmp / 10;
			sum[cnt++] = tmp % 10;
		}
		while (t != 0){
			tmp = t;
			t = (sum[cnt] + tmp) / 10;
			sum[cnt] = (sum[cnt] + tmp) % 10;
			cnt++;
		}
	}
	for (i = 1001; sum[i] == 0; i--);
	while (i >= 0)
		printf("%d", sum[i--]);
	puts("");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值