JAVA大數的一些操做

參考:html

http://www.javashuo.com/article/p-qtcllgbq-hs.htmlide

http://www.javashuo.com/article/p-xfizplvg-hv.htmlspa

 

(double保留兩位小數: System.out.println(String.format("%.2f",s));code

 

1,大數的輸入。orm

Scanner input = new Scanner(System.in);

BigInteger a = input.nextBigInteger();

 

2,字符串轉大數。htm

String s = "12345678987654321";

BigInteger a = new BigInteger(s);
BigInteger b
=BigInteger.valueOf(s,10);//將字符串轉換成10進制的大數

 

3,數字轉大數。blog

int a =123456;

BigInteger a = BigInteger.valueOf(a);

 

4,大數的四則運算。ip

a.add(b)    //求a+b 加法

a.subtract(b)  //求a-b 減法

a.divide(b)   //求a/b 除法

a.multiply(b)  //求a*b 乘法

 

5,其餘運算。字符串

a.mod(b)  //求餘數即a%b

a.gcd(b)   //求最大公約數

a.max(b)  //求最大值

a.min(b)   //求最小值

a.pow(b)  //求a^b的大數

  a.toString().length();  //求大數的長度get

 

6:留個坑,大數的快慢如何呢?

相關文章
相關標籤/搜索