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> #include<stdlib.h> const int N=1001; char s1[N],s2[N]; int a1[N],a2[N],b[N*2]; int main() { gets(s1); gets(s2); int j; memset(a1,0,sizeof(a1)); memset(a2,0,sizeof(a2)); memset(b,0,sizeof(b)); int len1=strlen(s1); int len2=strlen(s2); j=0; for(int i=len1-1;i>=0;i--) a1[j++]=s1[i]-'0'; j=0; for(int i=len2-1;i>=0;i--) a2[j++]=s2[i]-'0'; for(int i=0;i<len1;i++) for(int j=0;j<len2;j++) b[i+j]+=a1[i]*a2[j]; for(int i=0;i<N*2;i++) { if(b[i]>=10) { b[i+1]+=b[i]/10; b[i]%=10; } } bool flag=false; for(int i=N*2-1;i>=0;i--) { if(flag) printf("%d",b[i]); else if(b[i]) { printf("%d",b[i]); flag=true; } } if(!flag) printf("0"); return 0; }