题目描述 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;
}