蓝桥杯中好用的api

60 阅读3分钟

1.进制转换:

在刷题的时候遇到一些进制转换,自己写的话很费时间,java中现成的可以直接调用

//int转化为radix进制
Integer.toString(int num, int radix)
//long转化为radix进制
Long.toString(long num, int radix)

由于它们转化的都是字符串(少部分进制包含了字母),那么只需要对字符串进行处理

2.精度问题:

有一类题目就是给你很大的数据进行运算,没有任何算术类型可以存储,只可以用string来存储

它们的运算就要对字符串进行操作很麻烦 java中也有相对应的处理 biginteger

BigInteger bigInteger = new BigInteger("12421421421421421");
//加2
bigInteger = bigInteger.add(BigInteger.valueOf(2));
//减二
bigInteger = bigInteger.subtract(BigInteger.valueOf(2));
//乘2
bigInteger = bigInteger.multiply(BigInteger.valueOf(2));
//除2
bigInteger = bigInteger.divide(BigInteger.valueOf(2));
//模2222
bigInteger = bigInteger.mod(BigInteger.valueOf(2222));
System.out.println(bigInteger);

还有一类是浮点数精度

在 Java 中,floatdouble 类型是近似值类型,它们有精度限制,特别是在进行多次运算时,容易发生误差。BigDecimal 类提供了任意精度的小数运算,避免了浮动小数精度丢失的问题。

BigDecimal 支持不同的舍入模式和精度控制,能够满足特定场景下的需求,例如四舍五入、向上舍入、向下舍入等。

import java.math.BigDecimal;

public class BigDecimalExample {
    public static void main(String[] args) {
        BigDecimal num1 = new BigDecimal("10.12345");
        BigDecimal num2 = new BigDecimal("5.98765");

        // 加法运算
        BigDecimal resultAdd = num1.add(num2);
        System.out.println("加法结果: " + resultAdd);

        // 减法运算
        BigDecimal resultSub = num1.subtract(num2);
        System.out.println("减法结果: " + resultSub);

        // 乘法运算
        BigDecimal resultMul = num1.multiply(num2);
        System.out.println("乘法结果: " + resultMul);

        // 除法运算,设置精度和舍入方式
        BigDecimal resultDiv = num1.divide(num2, 5, BigDecimal.ROUND_HALF_UP);//四舍五入
        System.out.println("除法结果: " + resultDiv);
    }
}

******* **当然保留几位小数/四舍五入 也不需要这么麻烦,比如保留n为小数我们就在该浮点数的第n+1位小数添加5(因为要是那位小数>5那么必然会前一位进1,如果<5则+5还是最后会被舍弃)

复杂排序:

大多数时候有很多属性是绑定的,但是我们需要其中一个数据排序,比如people按照年龄排序

我们需要再类中实现一个接口Comparable或Comparator

它们都需要实现一个compareTo 函数

class Person implements Comparable<Person> {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public int compareTo(Person other) {
        return Integer.compare(this.age, other.age); // 按照年龄排序,由小到大.参数值换一下位置就是大到下
    }

}
List<Person> people = new ArrayList<>();
people.add(new Person("Alice", 30));
people.add(new Person("Bob", 25));
people.add(new Person("Charlie", 35));
Collections.sort(people); // 使用默认的 compareTo() 方法排序

如果是数组就用arrays.sort'

另外一种..........略

日期问题:

这类问题很常见,问你哪个时间是星期几,多少天之后日期,两个时间段之间怎么怎么的

平时写业务用到的一个localtime或者localdatatime就非常nice

LocalDateTime strtime = LocalDateTime.of(1970, 1, 1, 0, 0, 0); //年,月,日,时,分,秒
LocalDateTime localDateTime = LocalDateTime.of(2025, 1, 1, 0, 0, 0);
while (strtime.isBefore(localDateTime)) {
    //获取年月日
    int year = strtime.getYear();
    int month = strtime.getMonthValue();
    int day = strtime.getDayOfMonth();
    //星期几
    int week = strtime.getDayOfWeek().getValue();
    //往后一天
     strtime=strtime.plusDays(1);
}

这样我们就可以回去1970-2025的每一天是星期几,不需要去考虑闰年了, 当然这个也可以判断一个日期是否合法,传入不合法日期会报错,使用try{}catch


优先队列******

这是一种数据结构哈,这里只讲作用就是队列中会自动按照指定排序方式排序(默认从小到大)

一般处理那种需要实时获取最大值进行处理,就比如每次操作对最大值进行减半处理

//从大到小排序
PriorityQueue<Integer> q = new PriorityQueue<>((o1, o2) -> o2-o1);

for (int i = 0; i < 100000; i++){
    q.add(i);//入队
}
//操作五次
for (int i = 0; i < 5; i++) {
    Integer poll = q.poll();
    q.add(poll/2);
}

这样就不需要每次修改后重新排序了