大整数乘法——分治法

一。最原始的方法:

import java.util.Scanner;
public class Big {
    static int N=100;  
    static int a[]=new int[N];
    static int b[]=new int[N];  
    static int c[]=new int[2*N];  
    static String s1=new String();   
    static String s2=new String();   

    public static void main(String[] args) {
        Big demo=new Big();  
        demo.Input();  
        demo.Multiply(a, b, c);  
        demo.Output(); 
    }

    /*输出*/
    private void Output() { 

        System.out.println("result=");  
        int flag=2*N-1;  

        while(c[flag]==0) {  
            if(flag==0) {  
                System.out.println("0");  
                return ;  
            }  
            flag--;  
        }  

        for(int i=flag;i>=0;i--) {  
            System.out.print(c[i]);  
        }  

        System.out.println("");  
    }  
private void Multiply(int a[],int b[],int c[]) { 

        //逐个相乘
        for(int i=0;i<N;i++) {  
            for(int j=0;j<N;j++) {  
                c[i+j]+=a[i]*b[j];  
             }  
        }  

        //移位、进位
        for(int i=0;i<2*N-1;i++) {  
            c[i+1]+=c[i]/10;  
            c[i]=c[i]%10;  
        }  
    }  

    private void Input() {  
        Scanner scanner=new Scanner(System.in);  
        System.out.println("input two big data:");  
        s1=scanner.nextLine();  
        s2=scanner.nextLine();  
        GetDigit(s1, a);  
        GetDigit(s2, b);  
    }  

    private static void GetDigit(String s,int a[]) {  

        int len=s.length();  

        for(int i=0;i<len;i++) {  
            a[len-1-i]=s.charAt(i)-'0';  
        }  
    }
}

二。根据分治法来求

import java.util.Scanner;
import java.math.BigInteger;



class BigIntegerMultiply {

    private String number1, number2, result;

    public BigIntegerMultiply() {
        // TODO 自动生成构造函数存根
    }

    public BigIntegerMultiply(String number1, String number2) {
        super();
        this.number1  =  number1;
        this.number2  =  number2;
    }

    /*得到字符长度 * 1.保证字符长度是2的倍数 * */
    private int getlength(String str){

        int oldLength = str.length();
        int newLength = 1;

        while(newLength<oldLength) {
            newLength *= 2;
        }
        return newLength;
    }

    private boolean check(int len){
        while(len%2 != 1){
            len /= 2;
        }
        return len == 1;
    }

    private String initNumber(String str, int len){
        StringBuffer res  =  new StringBuffer("");
        int n  =  len-str.length();
        for(int i = 0; i<n; ++i) {
            res.append('0');
        }
        res.append(str);
        return res.toString();
    }

    private String getAnswer(String str1, String str2) {
        int len  =  str1.length();
        if(check(len)){
            // 该算法
            if(len <= 1){
                BigInteger num1 = new BigInteger(str1);
                BigInteger num2 = new BigInteger(str2);
                return num1.multiply(num2).toString();
            }
            //将X和Y分为两段
            String xLeft, yLeft, xRight, yRight;

            StringBuffer strBuf  =  new StringBuffer("");
            xLeft = str1.substring(0, len/2);
            xRight = str1.substring(len/2, len);
            yLeft = str2.substring(0, len/2);
            yRight = str2.substring(len/2, len);


            String num1, num2, num3;
            // num1 AC,即xLeft*yLeft
            num1 = getAnswer(xLeft, yLeft).toString();
            System.out.println("数字1:"+num1);

            // num2 BD,即xRight*yRight
            num2 = getAnswer(xRight, yRight).toString();
            System.out.println("数字2:"+num2);

            // 求num3:即AD+BC = (A+B)*(C+D) - AC - BD
            //先求出(xLeft+xRight) * (yLeft + yRight)
            //
            BigInteger big1 = new BigInteger(xLeft);
            BigInteger big2 = new BigInteger(yLeft);              

            big1 = big1.add(new BigInteger(xRight));
            big2 = big2.add(new BigInteger(yRight));
            System.out.println("大数1:"+big1);
            System.out.println("大数2:"+big2);
            BigIntegerMultiply big = new BigIntegerMultiply(big1.toString(), big2.toString());
            big.start(); 
            num3 = big.getResult();
            System.out.println(num3);
            //结果减去AC减去BD
            BigInteger num = new BigInteger(num3);
            num = num.subtract(new BigInteger(num1));
            num = num.subtract(new BigInteger(num2));
            num3 = num.toString();
            System.out.println("数字3:"+num3);

            ////
            //重新设置num1,在其后补上len个0
            strBuf.setLength(0); 
            strBuf.append(num1);
            for(int i=0; i<len; ++i) {
                strBuf.append('0');
            }
            num1=strBuf.toString();
            System.out.println("a:"+num1);

            //重新设置num3,在其后补上len/2个0
            strBuf.setLength(0); 
            strBuf.append(num.toString());
            for(int i=0; i<len/2; ++i) {
                 strBuf.append('0');
            }
            num3 = strBuf.toString();

            //结果为:num1+num3+num2
            BigInteger res = new BigInteger(num1);
            res = res.add(new BigInteger(num3));
            res = res.add(new BigInteger(num2));
            return res.toString();
        }
        return null;
    }

    public void start(){
        String str1, str2;
        int len1, len2, len;

        //保证两个数的位数一致,不一致的调用initNumber前面补0
        len1 = getlength(number1);
        len2 = getlength(number2);
        len = Math.max(len1, len2);

        str1 = initNumber(number1, len);    
        str2 = initNumber(number2, len);


        result  =  getAnswer(str1, str2);
    }

    //用来获取和设置成员变量
    public String getNumber1() {
        return number1;
    }

    public void setNumber1(String number1) {
        this.number1  =  number1;
    }

    public String getNumber2() {
        return number2;
    }
    public void setNumber2(String number2) {
        this.number2  =  number2;
    }

    public String getResult() {
        return result;
    }
    public void setResult(String result) {
        this.result = result;

    }

}


public class Big {

    public static void main(String[] args) {
        String str;
        Scanner scanner = new Scanner(System.in);
        BigIntegerMultiply big = new BigIntegerMultiply();

        while (scanner.hasNext()) {
            str = scanner.next();
            big.setNumber1(str);
            str = scanner.next();
            big.setNumber2(str);
            big.start();
            System.out.println(big.getResult());
        }
    }
}
    原文作者:大整数乘法问题
    原文地址: https://blog.csdn.net/qw963895582/article/details/48594831
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞