BigInteger的应用

例题

在这里插入图片描述

import java.math.BigInteger;
import java.util.*;
public class Main{
	public static void main(String[] args) {
		BigInteger n = BigInteger.valueOf(9999);
		String s = f(n).toString(2);
		System.out.println(s.length());
	}
	
	public static BigInteger f(BigInteger n) {
		if(n.equals(BigInteger.ONE)) return n;
		return n.multiply(f(n.subtract(BigInteger.ONE)));
	}
}

BigInteger常用方法

  • add()方法 : 大整数加法
	BigInteger a = new BigInteger("6");
	BigInteger b = new BigInteger("3");
	System.out.print("a+b="+a.add(b));//output : a+b=9
  • subtract()方法 : 大整数减法
	BigInteger c = new BigInteger("32");
	BigInteger d = new BigInteger("20");
	System.out.print("c-d="+c.subtract(d));//output : c-d=12
  • multiply()方法 : 乘法 (用法同上)
  • divide()方法 : 除法 (用法同上)
  • toString(byte)进制转化

关于BigInteger初始化为null

BigInteger是Java中的一个类,用于处理大整数运算。在Java中,BigInteger的初始值是null,而不是0。当你创建一个BigInteger对象时,如果没有显式地给它赋初值,它将被默认设置为null。

这是因为BigInteger是一个引用类型,而不是基本数据类型。引用类型的默认值是null,表示该引用没有指向任何对象。

如果你想将BigInteger的初始值设置为0,你可以使用BigInteger的构造函数来实现:

BigInteger num = new BigInteger(“0”);

这样就可以将num的初始值设置为0了

compareTo()方法 : 返回一个int型数据(1 大于; 0 等于 ; -1 小于)

BigInteger.ZERO //0
BigInteger.ONE	//1
BigInteger.TEN	//10
int x = bb.compareTo(BigInteger.ZERO);
//如果 bb > 0 --->x = 1
//如果 bb = 0 --->x = 0
//如果 bb < 0 --->x = -1

相关推荐

  1. BIgInteger和BigDecimal

    2024-04-01 04:14:01       57 阅读
  2. C#:用 BigInteger 计算 斐波那契数列

    2024-04-01 04:14:01       39 阅读
  3. uniapp定时器应用

    2024-04-01 04:14:01       55 阅读
  4. 顺序表应用

    2024-04-01 04:14:01       57 阅读
  5. Nacos应用

    2024-04-01 04:14:01       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-01 04:14:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-01 04:14:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-01 04:14:01       87 阅读
  4. Python语言-面向对象

    2024-04-01 04:14:01       96 阅读

热门阅读

  1. day 2| 上下文Context

    2024-04-01 04:14:01       39 阅读
  2. 索引+地址值

    2024-04-01 04:14:01       36 阅读
  3. UOS部署oceanbase

    2024-04-01 04:14:01       42 阅读
  4. C++中的结构体数组

    2024-04-01 04:14:01       38 阅读
  5. git删除分支

    2024-04-01 04:14:01       34 阅读
  6. 【八股】Spring MVC

    2024-04-01 04:14:01       36 阅读