POJ 2389 Bull Math(大数相乘)

Bull Math
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13920   Accepted: 7192

Description

Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers ... or so they say. Farmer John wonders if their answers are correct. Help him check the bulls' answers. Read in two positive integers (no more than 40 digits each) and compute their product. Output it as a normal number (with no extra leading zeros). 

FJ asks that you do this yourself; don't use a special library function for the multiplication.

Input

* Lines 1..2: Each line contains a single decimal number.

Output

* Line 1: The exact product of the two input lines

Sample Input

11111111111111
1111111111

Sample Output

12345679011110987654321











#include<stdio.h>
#include<string.h>
int main()
{
char a[45],b[45];
int s[90];
while(scanf("%s%s",a,b)!=EOF)
{
int i,j,k,s1,s2;
s1=strlen(a);
s2=strlen(b);
for(i=0; i<s1+s2; i++)
s[i]=0;
for(i=0; i<s1; i++)
for(j=0; j<s2; j++)
{
s[i+j+1]=s[i+j+1]+(a[i]-'0')*(b[j]-'0');
}
for(i=s1+s2-1; i>=0; i--)
if(s[i]>=10)
{
s[i-1]=s[i-1]+s[i]/10;
s[i]=s[i]%10;
}
i=0;
while(s[i]==0)
i++;
for(; i<s1+s2; i++)
printf("%d",s[i]);
printf("\n");
}
return 0;
}









posted on 2015-11-03 09:47  时间改变一切  阅读(251)  评论(0编辑  收藏  举报

导航