zoukankan      html  css  js  c++  java
  • 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 } 
  • 相关阅读:
    面试题27:二叉树的镜像
    面试题26:树的子结构
    面试题25:合并两个排序的链表
    面试题24:反转链表
    面试题23:链表中环的入口节点
    面试题22:链表中倒数第k个节点
    欧拉函数的使用
    C++ STL 全排列函数详解
    Ubuntu系统安装网易云音乐、搜狗输入法
    Ubuntu系统 安装谷歌 Chrome 浏览器
  • 原文地址:https://www.cnblogs.com/yzhhh/p/10473245.html
Copyright © 2011-2022 走看看