iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >关于Java大整数运算之BigInteger类
  • 211
分享到

关于Java大整数运算之BigInteger类

JavaBigInteger类Java大整数运算 2023-05-19 11:05:56 211人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录一、BigInteger类简单介绍二、BigInteger构造方式(1)构造方式(2)输入方式三、BigInteger常见的成员方法(1)方法介绍(2)方法使用演示1.加减乘除余

一、BigInteger类简单介绍

我们都知道Integer的存储范围是-2^31~2^31-1(-2147483648~2147483647),当我们要存储比Integer更大的数字时,java中就为我们提供了一个BigInteger类,方便我们去处理更大的数。

BigInteger 类支持任意精度的整数,也就是说在运算中 BigInteger 类可以准确地表示任何大小的整数值。首先除了基本的操作加、减、乘、除,在该类中还封装了其他很有用的操作,接下来将一一介绍。

二、BigInteger构造方式

(1)构造方式

很显然,BigInteger是一个封装类,就跟String类是一样的。使用时需要导入import java.math.BigInteger;使用 BigInteger 类,首先要创建一个 BigInteger 对象。BigInteger是一个有参构造,需要传入一个参数,最常见的就是给定一个字符串,使用构造方法public BigInteger(String val)构造一个十进制的BigInteger对象。

小贴士:该构造方法可以发生NumberFormatException异常,当字符串参数val中如果含有非数字字符就会发生该异常。

import java.math.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();

循环输入 :

 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)返回当前大整数对象与参数指定的大整数对象的和。
public BigInteger subtract(BigInteger val)返回当前大整数对象与参数指定的大整数对象的差。
public BigInteger multiply(BigInteger val)返回当前大整数对象与参数指定的大整数对象的积。
public BigInteger divide(BigInteger val)返回当前大整数对象与参数指定的大整数对象的商。
public BigInteger remainder(BigInteger val)返回当前大整数对象与参数指定的大整数对象的余。
public int compareTo(BigInteger val)返回当前大整数对象与参数指定的大整数的比较结果, 返回值是1、-1或0,分别表示当前 大整数对象大于、小于或等于参数指定的大整数。
public BigInteger abs()返回当前大整数对象的绝对值。
public BigInteger pow(int a)返回当前大整数对象的a次幂。
public String toString()返回当前大整数对象十进制的字符串表示。
public String toString(int p)返回当前大数对象p进制的字符串表示。

(2)方法使用演示

1.加减乘除余

import java.math.BigInteger;
import java.util.Scanner;

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));//余	
	}
}

看结果对于超大数字,也是完美计算结果的。

2.比较

import java.math.BigInteger;
import java.util.Scanner;

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返回结果为1。

3.绝对值和幂

import java.math.BigInteger;
import java.util.Scanner;

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.转换成对应进制字符串

import java.math.BigInteger;
import java.util.Scanner;

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不常见的成员方法

上面是我们经常会使用的一些常见方法,当然BigInteger中还有许多好用但不常见的方法,现整理分享给大家!

下面的方法都是指二进制

方法名含义
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 ,必须是正数
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() 寻找到第一个不为零数的 0的个数。如7->0111,则是4
public int bitLength()返回位长,不包含符号位。如7->0111,则是3
public int bitCount()补码表中和符号位不同的个数。如7->0111,则是3

到此这篇关于关于Java大整数运算之BigInteger类的文章就介绍到这了,更多相关Java大整数BigInteger类内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: 关于Java大整数运算之BigInteger类

本文链接: https://www.lsjlt.com/news/213124.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作