欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(guān)于Java大整數(shù)運(yùn)算之BigInteger類

 更新時間:2023年05月08日 11:25:12   作者:小熊愛吃軟糖吖  
這篇文章主要介紹了關(guān)于Java大整數(shù)運(yùn)算之BigInteger類,BigInteger提供高精度整型數(shù)據(jù)類型及相關(guān)操作,所有操作中,都以二進(jìn)制補(bǔ)碼形式表示,需要的朋友可以參考下

一、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)

    基于Java?SpringBoot的前后端分離信息管理系統(tǒng)的設(shè)計和實現(xiàn)

    當(dāng)今社會,人才的流動速度大大增加,因此也對黨建工作的管理層面工作帶來了空前且復(fù)雜的挑戰(zhàn),從而使得如何高效的開展管理黨建工作成為了亟待解決的問題。本文將介紹通過Java?SpringBoot實現(xiàn)前后端分離信息管理系統(tǒng),感興趣的同學(xué)可以了解一下
    2021-11-11
  • SpringCloud GateWay動態(tài)路由用法

    SpringCloud 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-10
  • protobuf簡介及使用流程

    protobuf簡介及使用流程

    本文介紹了Protocol Buffers(protobuf)的數(shù)據(jù)結(jié)構(gòu)序列化和反序列化框架,包括其特點(diǎn)、使用流程和快速上手,通過一個簡單的通訊錄示例,展示了如何創(chuàng)建.proto文件、添加注釋、編寫消息定義、編譯.proto文件以及進(jìn)行序列化和反序列化操作,感興趣的朋友一起看看吧
    2025-02-02
  • IDEA關(guān)于.properties資源文件的編碼調(diào)整問題

    IDEA關(guān)于.properties資源文件的編碼調(diào)整問題

    這篇文章主要介紹了IDEA關(guān)于.properties資源文件的編碼調(diào)整問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Springboot注解之@EnableAutoConfiguration詳解

    Springboot注解之@EnableAutoConfiguration詳解

    這篇文章主要介紹了Springboot注解之@EnableAutoConfiguration詳解,@EnableAutoConfiguration是一個加載Starter目錄包之外的需要Spring自動生成bean對象,本文對其進(jìn)行總結(jié),需要的朋友可以參考下
    2023-08-08
  • Java中Volatile關(guān)鍵字詳解及代碼示例

    Java中Volatile關(guān)鍵字詳解及代碼示例

    這篇文章主要介紹了Java中Volatile關(guān)鍵字詳解及代碼示例,分為兩個部分,第一部分介紹了Volatile關(guān)鍵字的基本概念等內(nèi)容,第二部分分享了實例代碼,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • 實例講解Java的Spring框架中的控制反轉(zhuǎn)和依賴注入

    實例講解Java的Spring框架中的控制反轉(zhuǎn)和依賴注入

    這篇文章主要介紹了Java的Spring框架中的控制反轉(zhuǎn)和依賴注入,Spring是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下
    2016-02-02
  • Java導(dǎo)出txt文件的方法

    Java導(dǎo)出txt文件的方法

    這篇文章主要介紹了Java導(dǎo)出txt文件的方法,實例分析了兩種java導(dǎo)出txt文本文件的使用技巧,需要的朋友可以參考下
    2015-05-05
  • java中out.print和out.write的方法

    java中out.print和out.write的方法

    本文用一個小例子說明java out.print和out.write的方法,大家參考使用吧
    2013-11-11
  • 配合Swagger使用絕佳的兩款直觀易用JSON可視化工具

    配合Swagger使用絕佳的兩款直觀易用JSON可視化工具

    這篇文章主要為大家介紹了配合Swagger使用絕佳的兩款直觀易用JSON可視化工具圖文詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06

最新評論