Blue Bridge Cup java skills summary


The day after tomorrow is the Blue Bridge Cup National Competition, record the basic knowledge that java may use, the time is in a hurry, if there is any mistake, welcome to criticize and correct

Previous real questions of the Blue Bridge Cup Competition

1. Data structure

Stacks and queues can be implemented with Linkedlist<>

1. Hash table

Divided into HashSet and HashMap

Set<Integer> set=new HashSet<Integer>();
set.add(1);//添加元素
set.remove(1);//删除元素
if(set.contains(1)) {
    
    ...}//判断是否包含某个元素
int len=set.size();//获得集合长度
Map<Character,Integer> map=new HashMap<Character,Integer>();
map.put('a', 1);
int num1=map.get('a');
int num2=map.getOrDefault('a',0);
map.replace('a',2);
map.remove('a');

2. Heap

Use priority queue (PriorityQueue) to achieve, the default is the small root heap

Queue<Integer> q=new PriorityQueue<Integer>();//创建一个小根堆
Queue<Integer> q=new PriorityQueue<Integer>((e1,e2)->e2-e1);//创建一个大根堆
//(e1,e2)->e2-e1代表升序,可以自定义其他排序规则

2. Object array sorting

Quickly sort an array of objects in ascending order, using the Arrays.sort() function

Arrays.sort(Object[] a,Comparator<? super T> c);//使用该参数时,排序的只能是对象数组
// 以第一列排序
int[][] a = new int[][]{
    
    {
    
    2, 1}, {
    
    1, 2}};
Arrays.sort(a, (e1, e2) -> e1[0] - e2[0]);
Integer[] i=new Integer[]{
    
    1,3,2};
Arrays.sort(i,(e1,e2)->e2-e1);//注意int数组只能转为Integer数组才能使用该表达式
int[][] a=new int[][]{
    
    {
    
    3,1},{
    
    3,2},{
    
    1,2}};
Arrays.sort(a,(e1,e2)->{
    
    
    if(e1[0]==e2[0])return e2[1]-e2[1];//若第一列相同按第二列排序
    return e1[0]-e2[0];//按第一列排序
});

3. Time related

Reference: https://blog.csdn.net/Mxeron/article/details/122798649

1. String to Date

import java.text.ParseException;
import java.text.SimpleDateFormat;
String s1 = "2021-7-24";
String s2 = "2022-7-24";
//yyyy-MM-dd,注意MM必须大写
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 转换成date1需要抛出异常
try {
    
    
	    Date date1 = sdf.parse(s1);
	} catch (ParseException e) {
    
    
	    e.printStackTrace();
}

2. Date to String (standard format)

Date date = new Date(120000000);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date));

3. Calender class (calendar, week)

The month MONTH of Calender starts from 0, that is, January-December corresponds to 0-11, but the date and year start from 1. DAY_OF_WEEK starts from 1, that is, Sunday to Saturday corresponds to 1-7.
Sunday is 1, Monday is 2, Saturday is 7. January is 0, December is 11.

// 创建Calendar实例
Calendar c = Calendar.getInstance();
// 用日期赋值2021年7月1日,或者是用Date赋值c.setTime(Date date);
c.set(2021, 7 - 1, 1);
System.out.println(c.get(Calendar.YEAR));//年
System.out.println(c.get(Calendar.MONTH) + 1);//月
System.out.println(c.get(Calendar.DATE));//日
System.out.println(c.get(Calendar.DAY_OF_WEEK));// 星期几,1是星期日
// 这年的第几天,从1开始算
System.out.println(c.get(Calendar.DAY_OF_YEAR));
// 这个月的第几天,从1开始算
System.out.println(c.get(Calendar.DAY_OF_MONTH));
// 这个月的第几周,从1开始算
System.out.println(c.get(Calendar.DAY_OF_WEEK_IN_MONTH));

4. Calculating time intervals

Mainly by using SimpleDateFormat, first write the time as a String, and then convert it into a Date, use the getTime function to convert it into milliseconds, and subtract it to get the difference in milliseconds. Note that 1s = 1000ms. In SimpleDateFormat, HH stands for 24-hour format, hh is 12-hour format, MM is month, and mm is minute.

String start = "2021-7-13 13:14:20";
String end = "2021-7-10 13:14:20";
// 注意HH是24小时,hh是12小时
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = sdf.parse(start);
Date date2 = sdf.parse(end);
// 因为不知道谁大谁小,所以要用abs
long diff = Math.abs(date1.getTime() - date2.getTime());
System.out.println("相差" + diff + "毫米");
// 注意1s = 1000ms
long seconds = diff / 1000;//秒
long minutes = diff / 1000 / 60;//分钟
long hours = diff / 1000 / 60 / 60;//小时
long day = diff / 1000 / 60 / 60 / 24;//天

4. String

1. Mutual conversion between int and String

//int转String
int i=1;  String s=""+i:
//String转int
String s=3;
int i=Integer.parseInt(s);

2. Determine whether a string is a palindrome

Method 1: Determine whether the i to j bits of the string are palindrome

 public static boolean isPalindrome(String s, int i, int j){
    
    
        while(i<j){
    
    
            if(s.charAt(i)!=s.charAt(j)){
    
    
                return false;
            }
            i++;j--;
        }
        return true;
    }

Method 2: Quickly judge whether a number is a palindrome

class Solution {
    
    
    public boolean isPalindrome(int x) {
    
    
        StringBuffer s=new StringBuffer(Integer.toString(x));
        String a=s.toString();//创建一个新字符串记录原来的值
        return a.equals(s.reverse().toString())?true:false;
    }
}

Five, BigInteger and BigDecimal

big integer problem

1.BigInteger

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

public class Main {
    
    
    public static void main(String[] args) {
    
    
        // 传入字符串才能形成大数,默认是把字符串当成十进制
        BigInteger bs = new BigInteger("15666666666666666");
        BigInteger bs1 = new BigInteger("100002123123123");
        //加减乘除
        bs.add(bs1);bs.subtract(bs1);bs.multiply(bs1);bs.divide(bs1);//取商        
        // 取余
        bs.mod(bs1);bs.remainder(bs1);
        // 返回大整数的double、float类型
        bs.doubleValue();bs.floatValue();
        // 求最大公约数
        bs.gcd(bs1);
        // 9、将当前大整数转成十进制字符串形式
        System.out.println(bs.toString());
        // 也可以把字符串当成二进制传入,生成十进制大数
        BigInteger bs2 = new BigInteger("100000", 2);
        System.out.println(bs2);
    }
}

2.BigDecimal

When adding, subtracting, and multiplying BigDecimal, the precision will not be lost, but when doing division, there are cases where it cannot be divisible. In this case, you must specify the precision and how to truncate.

package Chapter_5;

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

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner scan = new Scanner(System.in);
        // 大小数
        BigDecimal bs = scan.nextBigDecimal();
        // 获取小数位数,如果是整数则输出负数,表示末尾0的个数
        System.out.println(bs.scale());
        // 去掉小数末尾无用的0
        System.out.println(bs.stripTrailingZeros());
        // 设置小数位数,可以选择四舍五入或者直接截断
        System.out.println(bs.setScale(4, RoundingMode.HALF_UP));  // 四舍五入
        // 对BigDecimal做加、减、乘时,精度不会丢失,但是做除法时,存在无法除尽的情况,这时,就必须指定精度以及如何进行截断。
        BigDecimal d1 = new BigDecimal("123.456");
        BigDecimal d2 = new BigDecimal("23.456789");
        BigDecimal d3 = d1.divide(d2, 10, RoundingMode.HALF_UP); //保留10位小数并四舍五入
        BigDecimal d4 = d1.divide(d2); // 报错:ArithmeticException,因为除不尽
        //比较两个BigDecimal,不能用equals()因为小数的个数问题,要用compareTo()
        //它根据两个值的大小分别返回负数、正数和0,分别表示小于、大于和等于
        d1.compareTo(d2)
    }
}

6. Prime numbers and common divisors

Reference
https://blog.csdn.net/GD_ONE/article/details/104579936
https://blog.csdn.net/Mxeron/article/details/122798649

1. Determine whether a number is a prime number

Assuming that the number is n, we only need to judge whether there is a factor of n in [2, sqrt{n}]. If so, n is a composite number, otherwise, n is a prime number. This method is called trial division, which involves trying to divide all possible factors.

public static Boolean isprime(int n){
    
    
    if(n == 1) return false;
    for(int i = 2; i<= n/i ; i++){
    
    
        if(n % i == 0){
    
    
            return false;
        }
    }
    return true;
}

2. Find the greatest common divisor of two numbers

//递归(返回最大公约数)
int gcd(int a,int b){
    
    
    return b==0?a:gcd(b,a%b);
}

3. Decompose the prime factor

Let a prime number be p. If n%p == 0, then p is a prime factor of n, and the next step is to find the index of p. Let n = n/p, so that a p is removed from n, and then Repeat the above two steps until n%p! = 0

public static void prime(int n){
    
    
    for(int i = 2; i <= n/i ; i++){
    
    //判断条件用n/i,以防i*i<=n发生溢出
        int a = 0;//每次循环都要清零
        while(n % i == 0){
    
    
            n /= i;
            a++;
        }
        if(a > 0)
            System.out.println(i + "" + a);//i的a次方
    }
    //若该数是质数,那么应该将自身(n)也加入
    if(n > 1) System.out.println(n + " " + 1);
}

Seven, BFS and backtracking DFS framework

Backtracking DFS

Backtracking Algorithm Framework. Solving a backtracking problem is actually a traversal process of a decision tree. You only need to think about 3 questions:
1. Path: that is, the choice that has been made.
2. Choice list: that is, the choices you can make currently.
3. End condition: that is, the condition that reaches the bottom of the decision tree and cannot make any more choices.

代码方面,回溯算法的框架:
result = [] 
def backtrack(路径, 选择列表):
    if 满足结束条件:
        result.add(路径)
        return
    for 选择 in 选择列表:
        做选择
        backtrack(路径, 选择列表)
        撤销选择

BFS

In essence, it is a "graph" that allows you to go from a starting point to an end point and ask the shortest path. This is the essence of BFS.
The graph may go back, so use the visited array to store the nodes that have been visited.

/ 计算从起点 start 到终点 target 的最近距离
int BFS(Node start, Node target) {
    Queue<Node> q; // 核心数据结构
    HashSet<Node> visited; // 保存已经访问过的节点,避免走回头路
    q.offer(start); // 将起点加入队列
    visited.add(start);
    int step = 0; // 记录扩散的步数
    while (!q.isEmpty) {
        int sz = q.size();
        /* 将当前队列中的所有节点向四周扩散 */
        for (int i = 0; i < sz; i++) {
            Node cur = q.poll();
            /* 划重点:这里判断是否到达终点 */
            if (cur is target)
                return step;
            /* 将 cur 的相邻节点加入队列 */
            for (Node x : cur.adj())
                if (x not in visited) {
                    q.offer(x);
                    visited.add(x);
                }
        }
        /* 划重点:更新步数在这里 */
        step++;
    }
}

Guess you like

Origin blog.csdn.net/qq_41777702/article/details/125314089