大整數乘法——分治法

一。最原始的方法:

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());
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章