【bzoj1754/Usaco2005 qua】Bull Math——高精度乘法

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
 

高精乘高精模版......
代码:
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 char b1[45],a1[45];
 5 int a[45],b[45],ans[100];
 6 int main(){
 7     scanf("%s %s",a1+1,b1+1);
 8     a[0]=strlen(a1+1);b[0]=strlen(b1+1);
 9     for(int i=1;i<=a[0];i++)a[i]=a1[a[0]-i+1]-48;
10     for(int j=1;j<=b[0];j++)b[j]=b1[b[0]-j+1]-48;
11     for(int i=1;i<=a[0];i++){
12         for(int j=1;j<=b[0];j++){
13             ans[i+j-1]+=a[i]*b[j];
14             ans[i+j]+=ans[i+j-1]/10;
15             ans[i+j-1]%=10;
16         }
17     }
18     ans[0]=a[0]+b[0];
19     while(!ans[ans[0]]&&ans[0]>1)ans[0]--;
20     for(int i=ans[0];i>=1;i--)printf("%d",ans[i]);
21     return 0;
22 }
bzoj1754

 

posted @ 2017-10-13 20:56  Child-Single  阅读(183)  评论(0编辑  收藏  举报