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.
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> #define Len 43 //数的最大长度 int first[Len],second[Len],res[2*Len]; char a[Len],b[Len]; int lena,lenb; void Mul() { memset(res,0,sizeof(res)); int i,j,carry=0; for(i=1;i<=lena;i++) for(j=1;j<=lenb;j++) { res[i+j-1]+=(first[i]*second[j]); res[i+j]+=res[i+j-1]/10; res[i+j-1]%=10; } res[i+j]+=res[i+j-1]/10; res[i+j-1]%=10; for(i=lena+lenb;i>0&&res[i]==0;i--);//去前导0 for(;i>=1;i--) printf("%d",res[i]); printf("\n"); } int main() { //freopen("b.txt","r",stdin); while(scanf("%s %s",a,b)==2) { int i; lena=strlen(a),lenb=strlen(b); for(i=0;i<lena;i++) first[i+1]=a[lena-1-i]-'0'; for(i=0;i<lenb;i++) second[i+1]=b[lenb-1-i]-'0'; Mul(); } return 0; }
本文介绍了一个简单的程序,用于验证大型整数相乘的结果是否正确。通过手动实现乘法算法,该程序可以处理最多40位的整数,并输出其精确的乘积。
234

被折叠的 条评论
为什么被折叠?



