java大數

來源:互聯網
上載者:User

標籤:

java大數還是很好用的!

基本加入:

import java.math.BigInteger;

import jave.math.BigDecimal;

分別是大數和大浮點數。

首先讀入可以用:

Scanner input = new Scanner(System.in);

BigInteger a = new input.nextBigInteger();

這樣讀還是很方便的

當然還有自己建立:

BigInteger a = new BigInteger("1");

int b=1;

BigInteger c = BigInteger.ValueOf(b);

BigInteger d = BigInteger.ONE;

BigInteger e = c.add(a);

這類的定義方式,主要就是用字串可以定義,然後可以用ValueOf(值)這個函數,當然括弧內需要一個long型或者可直接轉換成long的值,可以用大數常數定義,可以用其他大數或者其他大數傳回值為大數的函數賦值。

java大數也有這類常數:

BigInteger.ONE;

BigInteger.TEN;

BigInteger.ZERO;

然後是重要的java大數的運算,基本的運算都比較簡單:

a.add(b);    // +

a.subtract(b);  // -

a.multiply(b);  // *

a.divide(b);   // /

a.remainder(b); 或 a.mod(b);  // %

a.pow(b);  // a的b次冪

a.abs();    // abs

a.negate();   // *(-1)

a.gcd(b);  // gcd

a.max(b);  a.min(b);  //max;min;

a.compareTo(b);  //a>b return 1;  a<b return -1;  a==b return 0;

java大數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.