POJ-2389-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

高精度乘法模板,注意不输出前导0。

 1 //高精度×高精度 
 2 #include<iostream>
 3 #include<cstring>
 4 using namespace std;
 5 int a[50],b[50],c[100];
 6 int la,lb,lc;
 7 int mult(int a[],int b[]){
 8     lc=la+lb+1;    //注意位数 
 9     for(int i=0;i<la;i++){
10         for(int j=0;j<lb;j++){
11             c[i+j]=c[i+j]+a[i]*b[j];
12             c[i+j+1]=c[i+j+1]+c[i+j]/10;
13             c[i+j]=c[i+j]%10;
14         }
15     }
16     if(!c[lc]) lc--;
17     return 0;
18     
19 }
20 
21 int main(){
22     
23     //读入
24     memset(a,0,sizeof(a));
25     memset(b,0,sizeof(b)); 
26     memset(c,0,sizeof(c));
27     string s1,s2;
28     cin>>s1>>s2;
29     la=s1.length();
30     lb=s2.length();
31     
32     for(int i=0;i<la;i++){
33         a[i]=s1[la-i-1]-'0';
34     }
35     for(int i=0;i<lb;i++){
36         b[i]=s2[lb-i-1]-'0';
37     }
38     
39     mult(a,b);
40     int flag=1;
41     for(int i=lc-1;i>=0;i--){
42         if(flag&&c[i]==0) continue;// 前导0不输出; 
43         else {
44             cout<<c[i];
45             flag=0; 
46         }    
47     }
48     
49     return 0;
50 } 

 

posted @ 2019-03-04 21:13  芹菜叶子  阅读(251)  评论(0编辑  收藏  举报