目录
起因是做了一道牛客oj题,链接指路☞三角形__牛客网,明明是一道超级简单的判断两边之和是否大于第三边,可是哪怕把数据类型改成long,仍然不能通过∑(っ°Д°;)っ。这时候就需要我们超级好用的大数字运算BigInteger啦。本文详细整理了BigInteger类的常见用法!欢迎享用(✪ω✪)~~~
我们都知道Integer的存储范围是-2^31~2^31-1(-2147483648~2147483647),当我们要存储比Integer更大的数字时,java中就为我们提供了一个BigInteger类,方便我们去处理更大的数。
BigInteger 类支持任意精度的整数,也就是说在运算中 BigInteger 类可以准确地表示任何大小的整数值。首先除了基本的操作加、减、乘、除,在该类中还封装了其他很有用的操作,接下来将一一介绍。
很显然,BigInteger是一个封装类,就跟String类是一样的。使用时需要导入 import java.math.BigInteger;使用 BigInteger 类,首先要创建一个 BigInteger 对象。BigInteger是一个有参构造,需要传入一个参数,最常见的就是给定一个字符串,使用构造方法public BigInteger(String val)构造一个十进制的BigInteger对象。
小贴士:该构造方法可以发生NumberFormatException异常,当字符串参数val中如果含有非数字字符就会发生该异常。
import java.math.BigInteger;
/*
* BigInteger演示
*/
public class Test {
public static void main(String[] args) {
BigInteger bigInteger=new BigInteger("1123");
}
}
普通输入:
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");
}
}
}
}
| 方法名 | 含义 |
| 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进制的字符串表示。 |
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));//余
}
}
看结果对于超大数字,也是完美计算结果的。

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返回结果为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.abs());
System.out.println(b.pow(100));
}
}

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中还有许多好用但不常见的方法,现整理分享给大家!
下面的方法都是指二进制
| 方法名 | 含义 |
| 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 |
如果觉得有用的话,就点个赞吧~

我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/
HashMap中为什么引入红黑树,而不是AVL树呢1.概述开始学习这个知识点之前我们需要知道,在JDK1.8以及之前,针对HashMap有什么不同。JDK1.7的时候,HashMap的底层实现是数组+链表JDK1.8的时候,HashMap的底层实现是数组+链表+红黑树我们要思考一个问题,为什么要从链表转为红黑树呢。首先先让我们了解下链表有什么不好???2.链表上述的截图其实就是链表的结构,我们来看下链表的增删改查的时间复杂度增:因为链表不是线性结构,所以每次添加的时候,只需要移动一个节点,所以可以理解为复杂度是N(1)删:算法时间复杂度跟增保持一致查:既然是非线性结构,所以查询某一个节点的时候
遍历文件夹我们通常是使用递归进行操作,这种方式比较简单,也比较容易理解。本文为大家介绍另一种不使用递归的方式,由于没有使用递归,只用到了循环和集合,所以效率更高一些!一、使用递归遍历文件夹整体思路1、使用File封装初始目录,2、打印这个目录3、获取这个目录下所有的子文件和子目录的数组。4、遍历这个数组,取出每个File对象4-1、如果File是否是一个文件,打印4-2、否则就是一个目录,递归调用代码实现publicclassSearchFile{publicstaticvoidmain(String[]args){//初始目录Filedir=newFile("d:/Dev");Datebeg
我基本上来自Java背景并且努力理解Ruby中的模运算。(5%3)(-5%3)(5%-3)(-5%-3)Java中的上述操作产生,2个-22个-2但在Ruby中,相同的表达式会产生21个-1-2.Ruby在逻辑上有多擅长这个?模块操作在Ruby中是如何实现的?如果将同一个操作定义为一个web服务,两个服务如何匹配逻辑。 最佳答案 在Java中,模运算的结果与被除数的符号相同。在Ruby中,它与除数的符号相同。remainder()在Ruby中与被除数的符号相同。您可能还想引用modulooperation.
Java的Collections.unmodifiableList和Collections.unmodifiableMap在Ruby标准API中是否有等价物? 最佳答案 使用freeze应用程序接口(interface):Preventsfurthermodificationstoobj.ARuntimeErrorwillberaisedifmodificationisattempted.Thereisnowaytounfreezeafrozenobject.SeealsoObject#frozen?.Thismethodretur
有没有一种简单的方法可以将给定的整数格式化为具有固定长度和前导零的字符串?#convertnumberstostringsoffixedlength3[1,12,123,1234].map{|e|???}=>["001","012","123","234"]我找到了解决方案,但也许还有更聪明的方法。format('%03d',e)[-3..-1] 最佳答案 如何使用%1000而不是进行字符串操作来获取最后三位数字?[1,12,123,1234].map{|e|format('%03d',e%1000)}更新:根据theTinMan的