關(guān)于Java大整數(shù)運(yùn)算之BigInteger類
一、BigInteger類簡單介紹
我們都知道Integer的存儲范圍是-2^31~2^31-1(-2147483648~2147483647),當(dāng)我們要存儲比Integer更大的數(shù)字時,java中就為我們提供了一個BigInteger類,方便我們?nèi)ヌ幚砀蟮臄?shù)。
BigInteger 類支持任意精度的整數(shù),也就是說在運(yùn)算中 BigInteger 類可以準(zhǔn)確地表示任何大小的整數(shù)值。首先除了基本的操作加、減、乘、除,在該類中還封裝了其他很有用的操作,接下來將一一介紹。
二、BigInteger構(gòu)造方式
(1)構(gòu)造方式
很顯然,BigInteger是一個封裝類,就跟String類是一樣的。使用時需要導(dǎo)入import java.math.BigInteger;
使用 BigInteger 類,首先要創(chuàng)建一個 BigInteger 對象。BigInteger是一個有參構(gòu)造,需要傳入一個參數(shù),最常見的就是給定一個字符串,使用構(gòu)造方法public BigInteger(String val)構(gòu)造一個十進(jìn)制的BigInteger對象。
小貼士:該構(gòu)造方法可以發(fā)生NumberFormatException異常,當(dāng)字符串參數(shù)val中如果含有非數(shù)字字符就會發(fā)生該異常。
import java.math.BigInteger; /* * BigInteger演示 */ public class Test { public static void main(String[] args) { BigInteger bigInteger=new BigInteger("1123"); } }
(2)輸入方式
普通輸入:
Scanner sc=new Scanner(System.in); BigInteger a=sc.BigIntegerNext();
循環(huán)輸入 :
while (sc.hasNextBigInteger()) { // 注意 while 處理多個 case }
除了定義實例,我們也可以像使用String類那樣使用BigInteger類,給大家一個題目感受一下,該題就是本文開頭所提及的三角形求解。
import java.math.BigInteger; import java.util.Arrays; import java.util.Scanner; /* * 三角形 */ public class Triangle { public static void main(String[] args) { Scanner sc=new Scanner(System.in); while (sc.hasNextBigInteger()) { // 注意 while 處理多個 case BigInteger []triangle=new BigInteger[3]; for(int i=0;i<3;i++) { triangle[i]=sc.nextBigInteger(); } Arrays.sort(triangle); if(triangle[0].add(triangle[1]).compareTo(triangle[2])>0) { System.out.println("Yes"); }else { System.out.println("No"); } } } }
三、BigInteger常見的成員方法
(1)方法介紹
方法名 | 含義 |
public BigInteger add(BigInteger val) | 返回當(dāng)前大整數(shù)對象與參數(shù)指定的大整數(shù)對象的和。 |
public BigInteger subtract(BigInteger val) | 返回當(dāng)前大整數(shù)對象與參數(shù)指定的大整數(shù)對象的差。 |
public BigInteger multiply(BigInteger val) | 返回當(dāng)前大整數(shù)對象與參數(shù)指定的大整數(shù)對象的積。 |
public BigInteger divide(BigInteger val) | 返回當(dāng)前大整數(shù)對象與參數(shù)指定的大整數(shù)對象的商。 |
public BigInteger remainder(BigInteger val) | 返回當(dāng)前大整數(shù)對象與參數(shù)指定的大整數(shù)對象的余。 |
public int compareTo(BigInteger val) | 返回當(dāng)前大整數(shù)對象與參數(shù)指定的大整數(shù)的比較結(jié)果, 返回值是1、-1或0,分別表示當(dāng)前 大整數(shù)對象大于、小于或等于參數(shù)指定的大整數(shù)。 |
public BigInteger abs() | 返回當(dāng)前大整數(shù)對象的絕對值。 |
public BigInteger pow(int a) | 返回當(dāng)前大整數(shù)對象的a次冪。 |
public String toString() | 返回當(dāng)前大整數(shù)對象十進(jìn)制的字符串表示。 |
public String toString(int p) | 返回當(dāng)前大數(shù)對象p進(jìn)制的字符串表示。 |
(2)方法使用演示
1.加減乘除余
import java.math.BigInteger; import java.util.Scanner; /* * BigInteger演示 */ public class Test { public static void main(String[] args) { Scanner sc=new Scanner(System.in); BigInteger a=sc.nextBigInteger(); BigInteger b=sc.nextBigInteger(); System.out.println(a.add(b));//加 System.out.println(a.subtract(b));//減 System.out.println(a.multiply(b));//乘 System.out.println(a.divide(b));//除 System.out.println(a.remainder(b));//余 } }
看結(jié)果對于超大數(shù)字,也是完美計算結(jié)果的。
2.比較
import java.math.BigInteger; import java.util.Scanner; /* * BigInteger演示 */ public class Test { public static void main(String[] args) { Scanner sc=new Scanner(System.in); BigInteger a=sc.nextBigInteger(); BigInteger b=sc.nextBigInteger(); System.out.println(a.compareTo(b)); } }
a>b返回結(jié)果為1。
3.絕對值和冪
import java.math.BigInteger; import java.util.Scanner; /* * BigInteger演示 */ public class Test { public static void main(String[] args) { Scanner sc=new Scanner(System.in); BigInteger a=sc.nextBigInteger(); BigInteger b=sc.nextBigInteger(); System.out.println(a.abs()); System.out.println(b.pow(100)); } }
4.轉(zhuǎn)換成對應(yīng)進(jìn)制字符串
import java.math.BigInteger; import java.util.Scanner; /* * BigInteger演示 */ public class Test { public static void main(String[] args) { Scanner sc=new Scanner(System.in); BigInteger a=sc.nextBigInteger(); BigInteger b=sc.nextBigInteger(); String strTen=a.toString(); String strTwo=b.toString(2); System.out.println(strTen); System.out.println(strTwo); } }
四、BigInteger不常見的成員方法
上面是我們經(jīng)常會使用的一些常見方法,當(dāng)然BigInteger中還有許多好用但不常見的方法,現(xiàn)整理分享給大家!
下面的方法都是指二進(jìn)制
方法名 | 含義 |
public BigInteger shiftLeft?(int n) | 左移一個比特位,*2 |
public BigInteger shiftRight?(int n) | 右移一個比特位,/2 |
public BigInteger and?(BigInteger val) | &和 |
public BigInteger or?(BigInteger val) | |或 |
public BigInteger xor?(BigInteger val) | ^異或 |
public BigInteger not() | ~取反 |
public BigInteger andNot?(BigInteger val) | &~ 先取和再取反 |
public boolean testBit?(int n) | 從0開始,第n位如果是1,則返回true,否則位false ,必須是正數(shù) |
public BigInteger setBit?(int n) | 將第n 位置1 |
public BigInteger clearBit?(int n) | 將第n 位置0 |
public BigInteger flipBit?(int n) | 如果第n為原來是1,則置0;如果第n為原來是0,則置1; |
public int getLowestSetBit() | 尋找到第一個不為零數(shù)的 0的個數(shù)。如7->0111,則是4 |
public int bitLength() | 返回位長,不包含符號位。如7->0111,則是3 |
public int bitCount() | 補(bǔ)碼表中和符號位不同的個數(shù)。如7->0111,則是3 |
到此這篇關(guān)于關(guān)于Java大整數(shù)運(yùn)算之BigInteger類的文章就介紹到這了,更多相關(guān)Java大整數(shù)BigInteger類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Java?SpringBoot的前后端分離信息管理系統(tǒng)的設(shè)計和實現(xiàn)
當(dāng)今社會,人才的流動速度大大增加,因此也對黨建工作的管理層面工作帶來了空前且復(fù)雜的挑戰(zhàn),從而使得如何高效的開展管理黨建工作成為了亟待解決的問題。本文將介紹通過Java?SpringBoot實現(xiàn)前后端分離信息管理系統(tǒng),感興趣的同學(xué)可以了解一下2021-11-11SpringCloud GateWay動態(tài)路由用法
網(wǎng)關(guān)作為所有項目的入口,不希望重啟,因此動態(tài)路由是必須的,動態(tài)路由主要通過RouteDefinitionRepository接口實現(xiàn),其默認(rèn)的實現(xiàn)是InMemoryRouteDefinitionRepository,即在內(nèi)存中存儲路由配置,可基于這個map對象操作,動態(tài)路由的實現(xiàn)方案有兩種2024-10-10IDEA關(guān)于.properties資源文件的編碼調(diào)整問題
這篇文章主要介紹了IDEA關(guān)于.properties資源文件的編碼調(diào)整問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06Springboot注解之@EnableAutoConfiguration詳解
這篇文章主要介紹了Springboot注解之@EnableAutoConfiguration詳解,@EnableAutoConfiguration是一個加載Starter目錄包之外的需要Spring自動生成bean對象,本文對其進(jìn)行總結(jié),需要的朋友可以參考下2023-08-08實例講解Java的Spring框架中的控制反轉(zhuǎn)和依賴注入
這篇文章主要介紹了Java的Spring框架中的控制反轉(zhuǎn)和依賴注入,Spring是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下2016-02-02