大整数乘法
Description
求两个不超过200位的非负整数的积。
Input
有两行,每行是一个不超过200位的非负整数,没有多余的前导0。
Output
一行,即相乘后的结果。结果里不能有多余的前导0,即如果结果是342,那么就不能输出为0342。
Sample Input
12345678900
98765432100
Sample Output
1219326311126352690000
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
int main(void)
{
void bigtime(char *, char *);
char a[5001], b[5001];
while(cin >> a >> b)
bigtime(a, b);
return 0;
}
void bigtime(char *a, char *b)
{
int time[20000] = {0}, t, k = 0;
int len_a = (int)strlen(a);
int len_b = (int)strlen(b);
for(int i = len_a-1; i >= 0; i--)
{
k = len_a-1-i;
for(int j = len_b-1; j >= 0; j--)
{
t = 0;
time[k] += (a[i]-'0') * (b[j]-'0');
if(time[k++] >= 10)
{
t = time[k-1] / 10;
time[k-1] %= 10;
}
time[k] += t;
}
}
int p = 0;
for( ; k >= 0; k--)
{
if(time[k] != 0)
p = 1;
if(k == 0 && p == 0)
cout << 0;
if(p)
cout << time[k];
}
}

浙公网安备 33010602011771号