原题:
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#include #define M 220 int a1[M],a2[M]; char s1[M],s2[M]; int p[M*2]; int main() { gets(s1); gets(s2); int i,j; int l1=strlen(s1); memset(a1,0,sizeof(a1)); memset(a2,0,sizeof(a2)); memset(p,0,sizeof(p)); j=0; for(i=l1-1; i>=0; i--) { a1[j++]=s1[i]-'0'; } j=0; int l2=strlen(s2); for(i=l2-1; i>=0; i--) { a2[j++]=s2[i]-'0'; } for(i=0; i =10) { p[i+1]+=p[i]/10; p[i]%=10; } } for(i=M*2-1; i>=0; i--) { if(p[i]) break; } for(int k=i; k>=0; k--) { printf("%d",p[k]); } printf("\n"); return 0; }