zoukankan      html  css  js  c++  java
  • 大数乘法

    大数乘法是一个很常见的问题,今年(2017年)参加拼多多内推,其中有一题就是大数乘法,今天写一下大数乘法的实现:

    思路就是模拟手工计算的一个普通算法,网上说的什么分治法和FFT算法目前我还没有尝试。

    把自己实现的代码贴出来

     1 import java.util.Scanner;
     2 
     3 /**
     4  * 大整数相乘
     5  *
     6  */
     7 
     8 public class bigNumberMulti {
     9 
    10     static char[] reverse(char[] ch){//字符反转,即123转为321
    11         int length = ch.length;
    12         char[] temp = new char[length];
    13         for(int i=0;i<length;i++){
    14             temp[length-1-i] = ch[i];            
    15         }
    16         
    17         
    18         return temp;
    19     }
    20     static void multi(char[] chr1,char[] chr2){//乘法运算
    21         int n = chr1.length;
    22         int m = chr2.length;
    23         chr1 = reverse(chr1);
    24         chr2 = reverse(chr2);
    25         int [] result = new int [m+n];
    26         for(int i=0;i<n;i++){        //暂时不处理进位,将i+j位的数累加起来,然后进行统一进位处理
    27             for(int j=0;j<m;j++){
    28                 int temp = (chr1[i]-48)*(chr2[j]-48);
    29                 result[i+j] += temp;
    30             }
    31         }
    32         
    33         for(int i=0;i<m+n-1;i++){    //处理进位
    34             int temp = result[i]/10;
    35             result[i] = result[i]%10;
    36             result[i+1] += temp;
    37         }
    38         int location = 0;            //记录最高位的位置
    39         for(int i=m+n-1;i>=0;i--){    //找到最高位的位置
    40             if(result[i]!=0){
    41                 location = i;
    42                 break;
    43             }
    44         }
    45         for(int i=location;i>=0;i--){ //打印结果
    46             System.out.print(result[i]);
    47         }
    48         
    49     }
    50     
    51     public static void main(String[] args) {
    52         // TODO Auto-generated method stub
    53         Scanner  sc = new Scanner(System.in);
    54         char [] chr1 = sc.next().toCharArray();
    55         char [] chr2 = sc.next().toCharArray();
    56         multi(chr1, chr2);
    57         
    58     }
    59 
    60 }
  • 相关阅读:
    ER图
    uml图
    第一个迭代任务的制作
    软件测试
    实训记录
    UML系列图——ER图
    UML系列图——用例图
    第一个迭代任务进度
    第一个迭代任务
    需求分析——WBS
  • 原文地址:https://www.cnblogs.com/skylv/p/7272684.html
Copyright © 2011-2022 走看看