zoukankan      html  css  js  c++  java
  • POJ2389: 大数字乘法算法

    2014-12-26

    大数字乘法算法一般是采用模拟"小学生乘法演算过程”方法。

    主要算法思想:

      1. 乘数a第i)位与乘数b第j)位数字相乘,并将该乘积结果放到乘积结果数组product的第(i+j-1)位中;

      2. 检查product的第(i+j-1)位中储存的数字是否超过或等于10,若是,则“取余并且进位”。

    细节参考代码:

     1 #include<iostream>
     2 #include<cstring>
     3 using namespace std;
     4 void solve(string a,string b){
     5     int a_length=a.length();
     6     int b_length=b.length();
     7     int *arr_a=new int[a_length+1];
     8     int *arr_b=new int[b_length+1]; 
     9     
    10     for(int i=1;i<=a_length;i++){
    11         arr_a[i] = a[i-1]-'0';
    12     }
    13     for(int i=1;i<=b_length;i++){
    14         arr_b[i] = b[i-1]-'0';
    15     }
    16     
    17     int *product=new int[a_length+b_length];
    18     int product_length = a_length+b_length-1;
    19     for(int i=0;i<=product_length;i++) product[i]=0;
    20      
    21      for(int i=a_length;i>=1;i--){
    22          
    23          for(int j=b_length;j>=1;j--){
    24              int temp = arr_a[i]*arr_b[j];
    25              int c = product[i+j-1]+temp;
    26              product[i+j-1] = c%10;
    27              product[i+j-2] += c/10;
    28          }
    29      }
    30      
    31 
    32     if(product[0]!=0) cout<<product[0];
    33     for(int i=1;i<=product_length;i++){
    34         cout<<product[i];
    35     }cout<<endl;
    36 }
    37 int main(){
    38     string a,b;
    39     while(cin>>a>>b){
    40         solve(a,b);
    41     }
    42     return 0;
    43 }
    View Code
  • 相关阅读:
    WebDriver关闭窗口
    WebDriver切换窗口
    WebDriver处理截屏
    获取当前窗口的地址栏url地址
    WebDriver获取当前窗口的title
    复选框
    勾选框
    单选框
    VUE 中获取摄像头进行扫码
    js byte 与 进制 相互转换
  • 原文地址:https://www.cnblogs.com/fu11211129/p/4186212.html
Copyright © 2011-2022 走看看