java中的BigInteger

來源:互聯網
上載者:User

標籤:小數   列印   sys   inter   範圍   print   printf   函數   絕對值   

標頭檔

import java.io.*;

import java.math.*;

 

讀入

Scanner cin = Scann(System.in);

while(cin.hasNext()) <-> !=EOF

n = cin.nextInt(); <-> 讀入一個int數

n = cin.nextBigInterger(); <-> 讀入一個大整數

 

輸出

System.out.println(n);    //列印n

System.out.println();      //換行

System.out.printf("%d\n",n);    //也可以類似c++裡的輸出方式

 

定義: 

int i,j,k,a[];

a = new int[100];

BigInteger n,m;

BigDecimal n;

String s;

 

資料類型:

資料類型 類型名 位長 取值範圍 預設值

布爾型 boolean 1 true,false false

位元組型 byte 8 -128-127 0

字元型 char 16 ‘\u000’-\uffff ‘\u0000’

短整型 short 16 -32768-32767 0

整型 int 32 -2147483648,2147483647 0

長整型 long 64 -9.22E18,9.22E18 0

浮點型 float 32 1.4E-45-3.4028E+38 0.0

雙精確度型 double 64 4.9E-324,1.7977E+308 0.0

 

BigInteger 任意大的整數,原則上是,只要你的電腦的記憶體足夠大,可以有無限位的

BigInteger 任意大的實數,可以處理小數精度問題。

BigInteger中一些常見的函數:

A=BigInteger.ONE

B=BigInteger.TEN

C=BigInteger.ZERO

一些常見的數的賦初值。將int型的數賦值給BigInteger,BigInteger.valueOf(k);

 

基本的函數:

valueOf:賦初值

add:+ a.add(b);

subtract:-

multiply:*

divide:/

remainder:this % val

divideAndRemainder:a[0]=this / val; a[1]=this % val

pow:a.pow(b)=a^b

gcd,abs:公約數,絕對值

negate:取負數

signum:符號函數

mod:a.mod(b)=a%b;

shiftLeft:左移,this << n ,this*2^n;

shiftRight:右移,this >> n,this/2^n;

and:等同於c++的&&,且;

or:||,或;

xor:異或,BigInteger xor(BigInteger val),this^val

not:!,非;

bitLength:返回該數的最小二進位補碼錶示的位的個數,即 *不包括* 符號位 (ceil(log2(this <0 ? -this : this + 1)))。對正數來說,這等價於普通二進位表示的位的個數。

bitCount:返回該數的二進位補碼錶示中不包擴符號位在內的位的個數。該方法在 BigIntegers 之上實現位向量風格的集合時很有用。

isProbablePrime:如果該 BigInteger 可能是素數,則返回 true ;如果它很明確是一個合數,則返回 false 。 參數 certainty 是對調用者願意忍受的不確定性度量:如果該數是素數的機率超過了 1 - 1/2**certainty方法,則該方法返回 true 。執行時間正比於參數確定性值。

compareTo:根據該數值是小於、等於、或大於 val 返回 -1、0 或 1;

equals:判斷兩數是否相等,也可以用compareTo來代替;

min,max:取兩個數的較小、大者;

intValue,longValue,floatValue,doublue:把該數轉換為該類型的數的值。

 

java中的BigInteger

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.