【蓝桥杯算法】Java的基础API

1. BigInteger 的使用

1.1. 判素数

复制代码
package 模板;

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

public class 判素数 {
    static Scanner in = new Scanner(System.in);

    public static void main(String[] args) {
        int q = in.nextInt();
        while (q-- > 0) {
            BigInteger a = new BigInteger(in.next());
            if (a.isProbablePrime(20)) {
                System.out.println(a);
            } else {
                System.out.println(a.nextProbablePrime());
            }
        }
    }
}

1.2. 快速幂

复制代码
package 模板;

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

public class 快速幂 {
    static Scanner in = new Scanner(System.in);

    public static void main(String[] args) {
        BigInteger b = new BigInteger(in.next());
        BigInteger p = new BigInteger(in.next());
        BigInteger k = new BigInteger(in.next());

        System.out.println(b.modPow(p, k));
    }
}

1.3. 求逆元

复制代码
package 模板;

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

public class 逆元 {
    static Scanner in = new Scanner(System.in);

    public static void main(String[] args) {
        int q = in.nextInt();
        while (q-- > 0) {
            BigInteger a = new BigInteger(in.next());
            BigInteger b = new BigInteger(in.next());
            System.out.println(a.modInverse(b));
        }
    }
}

2. Arrays

  1. 直接对于 int 对象排序使用的是快排
  2. 对于 Integer 对象排序使用的是归并排序
相关推荐
默默coding的程序猿10 分钟前
3.前端和后端参数不一致,后端接不到数据的解决方案
java·前端·spring·ssm·springboot·idea·springcloud
在未来等你24 分钟前
JVM调优实战 Day 15:云原生环境下的JVM配置
java·jvm·性能优化·虚拟机·调优
NAGNIP25 分钟前
一文搞懂FlashAttention怎么提升速度的?
人工智能·算法
funnycoffee12325 分钟前
Huawei 6730 Switch software upgrade example版本升级
java·前端·华为
Java初学者小白26 分钟前
秋招Day15 - Redis - 缓存设计
java·数据库·redis·缓存
缘来是庄26 分钟前
设计模式之组合模式
java·设计模式·组合模式
DKPT28 分钟前
Java组合模式实现方式与测试方法
java·笔记·学习·设计模式·组合模式
G探险者43 分钟前
《如何在 Spring 中实现 MQ 消息的自动重连:监听与发送双通道策略》
java·开发语言·rpc
Codebee1 小时前
OneCode图生代码技术深度解析:从可视化设计到注解驱动实现的全链路架构
css·人工智能·算法