Solution to group C of JAVA University in the 12th Blue Bridge Cup provincial competition

Posted by jcd on Wed, 26 Jan 2022 06:38:05 +0100

A: ASC


code

public class A_ASC {
    public static void main(String[] args) {
        System.out.println((int) 'L');
        // out: 76
    }
}

B: Space


code

public class B_space {
    public static void main(String[] args) {
        // 32 bits = 4 bytes 4B, 1MB = 1024KB = 1024 * 1024B
        System.out.println(256 * 1024 * 1024 / 4);
        // out: 67108864
    }
}

C: Card

thinking

Use an array to record the number of cards of each number from 0 to 9. Each time you traverse a number, subtract the card corresponding to each digit of the number until the number of a certain card is 0

code

public class C_card {

    private static boolean sub(int num, int[] A){
        while (num > 0){
            int mod = num % 10;
            if (A[mod] == 0) return false;
            else A[mod]--;
            num /= 10;
        }
        return true;
    }

    public static void main(String[] args) {
        int[] cnt = new int[10];
        Arrays.fill(cnt, 2021);
        int num = 1;
        while (sub(num, cnt)) num++;
        System.out.println(num - 1);
        // out: 3182
    }
}

D: Multiply

thinking

Direct violence

code

public class D_Multiply {
    public static void main(String[] args) {
        long mod = 1000000007;
        long res = 0;
        for (long i = 40000; i < mod; i++) {
            if (i * 2021 % mod == 999999999) {
                res = i;
                break;
            }
        }
        System.out.println(res);
        // out: 17812964
    }
}

E: Path

thinking

Greedy, it should be that the difference between the two paths is at most 21. When the number is large, the minimum common multiple will also be large. Therefore, keep each point as a multiple of a certain value to ensure that the minimum common multiple is kept at the minimum. After lax induction, it is concluded that the multiple of 21 each time can ensure the minimum common multiple. Of course, 21 is not a factor of 2021, Finally, there is a certain number to be multiplied by 2021, and the sum of the previous common multiples is the shortest path. (without strict proof, I can't guarantee whether it is correct 😂

code

public static void main(String[] args) {
        int i = 1;
        int n = 21;
        int total = 0;
        while (n <= 2021){
            System.out.println(n);
            total += i++ * n;
            n += 21;
        }
        System.out.println(total +  (n - 21) * 2021);
        // out: 10364592 Mongolian
    }

F: Time display

thinking

There's nothing to say. Just adjust the API directly, but note that the topic starts from 0 o'clock, and the Java API starts from 8 o'clock. Subtract eight hours from the given timestamp

code

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class F_Time display {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        DateFormat df = new SimpleDateFormat("HH:mm:ss");
        Date date = new Date(scan.nextLong() - 1000 * 60 * 60 * 8);
        String sTime = df.format(date);
        System.out.println(sTime);
    }
}

G: Minimum weight

thinking

Don't be fooled by the example. The essence of this question is binary, because each number can be represented by binary, and each bit of binary can represent the number on that bit. For example, the binary of number 7 is 111, and its weight represents the weight of weight bits 4, 2 and 1. These three binary bits can represent numbers below 7, for example, the binary of 8 is 1000, which means that four weights are needed, The weight is 8, 4, 2 and 1 respectively. So the result is the length after converting the integer to bit binary.

code

import java.util.Scanner;

public class G_Minimum weight {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        String s = Integer.toBinaryString(N);
        System.out.println(s.length());
    }
}

H: Yanghui triangle

thinking

Scroll the array, save the results of each layer with one-dimensional list, and traverse layer by layer

code

import java.util.*;

public class H_Yanghui triangle {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        List<Integer> lst = new ArrayList<>();
        lst.add(1);
        int res = 0;
        int row = 2;
        boolean flag = true;
        while (flag){
            for (Integer i : lst) {
                res++;
                if (i == N) {
                    System.out.println(res);
                    flag = false;
                    break;
                }
            }
            List<Integer> t = new ArrayList<>();
            for (int i = 0; i < row; i++) {
                if (i == 0 || i == row - 1) t.add(1);
                else   t.add(lst.get(i - 1) + lst.get(i));
            }
            row++;
            lst = t;
        }
    }
}

1: Left child right brother

thinking

Greedy + memory search, first store the child nodes of each node in the hash table, recursively find the child node with the highest height, and the maximum height of the whole tree is the number of child nodes plus the height of the longest child node. Find the longest child node and spell it to the end. Add the accessed nodes to the cache to avoid double calculation.

code

import java.util.*;

public class I_Left child right brother {
    static Map<Integer, List<Integer>> map = new HashMap<>();
    static Map<Integer, Integer> cache = new HashMap<>();
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        for (int i = 2; i <= N; i++) {
            int p = scan.nextInt();
            map.putIfAbsent(p, new ArrayList<>());
            map.get(p).add(i);
        }
        System.out.println(getMaxHigh(1) - 1);
        System.out.println(map);
    }

    static int getMaxHigh(int c){
        if (!map.containsKey(c)) cache.put(c, 1);
        if (cache.containsKey(c)) return cache.get(c);
        List<Integer> children = map.get(c);
        int size = children.size();
        int maxChildren = 0;
        for (Integer child : children) {
            maxChildren = Math.max(maxChildren, getMaxHigh(child));
        }
        cache.put(c, size + maxChildren);
        return cache.get(c);
    }
}

J: Bidirectional sorting

thinking

Directly adjust the API (properly timed out), and there is no other way to write it at present

code

import java.util.Arrays;
import java.util.Scanner;

public class J_Bidirectional sorting {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt(), m = scan.nextInt();
        Integer[] arr = new Integer[n];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = i + 1;
        }
        while (m-- != 0) {
            int p = scan.nextInt(), idx = scan.nextInt();
            if (p == 0) Arrays.sort(arr,  0, idx, (a, b) -> b - a);
            else Arrays.sort(arr, idx - 1, n);
        }
        for (Integer i : arr) {
            System.out.print(i + " ");
        }
    }
}

summary

It's still a little difficult. Please give me some advice

Topics: Java Algorithm