java大數運算(講解)


在算法競賽或者面試中我們經常遇到大數問題,例如求一個很大的階層,大數加法等等。

住在這種情況下我們用常規解法(使用long long或long long int)肯定是不行的,

而我們自己用c/c++寫一個大數的算法又過於麻煩且易於出錯,

在這種情況下使用java中自帶的大數類是我們最好的選擇,

相對比c/c++比較而言,java語言寫大數是比較流氓的,但是代碼量非常的少,而且容易理解,

你只需要調包就可以了。
BigInteger

package 大數;

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

public class 大數 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        BigInteger b1=new BigInteger("123456789");
        BigInteger b2=new BigInteger("987654321");
        System.out.println("加法操作:"+b2.add(b1));
        System.out.println("減法操作:"+b2.subtract(b1));
        System.out.println("乘法操作:"+b2.multiply(b1));
        System.out.println("除法操作:"+b2.divide(b1));
        System.out.println("最大數:"+b2.max(b1));
        System.out.println("最小數:"+b2.min(b1));
        BigInteger result[]=b2.divideAndRemainder(b1);
        System.out.println("商是:"+result[0]+" "+"余數是:"+result[1]);
    }

}

1.定義常用方法:

BigInteger a=new BigInteger(“123”);  //第一種,參數是字符串
BigInteger a=BigInteger.valueOf(123);    //第二種,參數可以是int、long

2.大整數比較大小

a.equals(b);  //如果a、b相等返回true否則返回false
a.compareTo(b);  //a小於b返回-1,等於返回0,大於返回1

3.大數常用方法及常量

a.mod(b);  //求余
 a.gcd(b);  //求最大公約數
 a.max(b);  //求最大值
 a.min(b);  //求最小值
BigInteger.ZERO    //大整數0
BigInteger.ONE    //大整數1
BigInteger.TEN   //大整數10
//先轉換成字符串再求字符串的長度
a.toString().length();   //a的類型為BigInteger

BigDecimal
使用此類可以完成大的小數操作,而且也可以使用此類進行精確的四舍五入,這一點在開發中經常使用。
對於不需要任何准確計算精度的程序可以直接使用float或double完成,但是如果需要精確計算結果,則必須使用BigDecimal類。

//最重要的方法
a.compareTo(b);  //比較兩個浮點數是否相等
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;

public class 大數 {
    public static void main(String[] args) {
        System.out.println("加法運算:" + MyMath.round(MyMath.add(10.345, 3.333), 1));
        System.out.println("減法運算:" + MyMath.round(MyMath.sub(10.345, 3.333), 3));
        System.out.println("乘法運算:" + MyMath.round(MyMath.mul(10.345, 3.333), 4));
        System.out.println("除法運算:" + MyMath.div(10.345, 3.333, 3));
    }
}

class MyMath {
    public static double add(double d1, double d2) { // 進行加法計算
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2 = new BigDecimal(d2);
        return b1.add(b2).doubleValue();
    }

    public static double sub(double d1, double d2) { // 進行減法計算
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2 = new BigDecimal(d2);
        return b1.subtract(b2).doubleValue();
    }

    public static double mul(double d1, double d2) { // 進行乘法計算
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2 = new BigDecimal(d2);
        return b1.multiply(b2).doubleValue();
    }

    public static double div(double d1, double d2, int len) { // 進行除法計算
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2 = new BigDecimal(d2);
        return b1.divide(b2, len, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static double round(double d, int len) { // 進行四舍五入
        BigDecimal b1 = new BigDecimal(d);
        BigDecimal b2 = new BigDecimal(1); // 技巧
        return b1.divide(b2, len, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
}

大數素數了解:

public static void main(String[] args)
    {
        Scanner cin = new Scanner(System.in);
        BigInteger x;
        x = cin.nextBigInteger();
        if(x.isProbablePrime(1))
            System.out.println("Yes");
        else
            System.out.println("No");
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM