2022年第十三届蓝桥杯国赛JavaB组-决赛部分题解

题目与部分参考答案

2022年第十三届蓝桥杯国赛JavaB组-决赛部分题解_第1张图片
这题要考虑一下一个小时分针与秒针重合的次数只有59次,没有60次。第x1:59:00~x1:59:59这六十秒里分针和秒针不会重合。
而到x2:00:00时会重合,但是这已经是另一个小时了。
答案:494(502 - 8)

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int hour = 6;
        int min = 13;
        int sec = 22;
        int ans = 0;
        while (hour != 14 || min != 36 || sec!= 20  ) {
            sec++;
            if (sec == 60) {
                sec = 0;
                min++;
                if (min == 60) {
                    min = 0;
                    hour++;
                }
            }
            if (sec == min) {
                ans++;
                System.out.println(hour + ":" + min + ":" + sec);
            }
        }
        ans -= 8;
        System.out.println(ans);
    }
}

2022年第十三届蓝桥杯国赛JavaB组-决赛部分题解_第2张图片

答案:25606
算不出来,仅供参考

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main {
    static int ans;
    static List<Integer> all;
    public static void main(String[] args) {
        ans = 0;
        all = getAll();
        recursion(0, 0, 1);
        System.out.println(ans);
        /*for (int i = 2333333; i <= 23333333; i++) {
            int cur = i;
            int count = 0;
            while (cur != 1) {
                for (int j = 2; j <= i; j++) {
                    if (cur % j == 0) {
                        cur /= j;
                        count++;
                        break;
                    }
                }
            }
            if (count == 12) {
                ans++;
            }
        }*/
        System.out.println(ans);
    }
    public static List<Integer> getAll() {
        int val = 23333;
        List<Integer> ans = new ArrayList<Integer>();
        for (int i = 2; i <= val; i++) {
            boolean flag = true;
            for (int j = 2; j < i; j++) {
                if (i % j == 0) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                ans.add(i);
            }
        }
        return ans;
    }
    public static void recursion(int count, int pre, int cur) {
        if (count >= 12) {
            if (cur <= 23333333) {
                ans++;
            }
            return;
        }
        for (int i = pre; i < all.size(); i++) {
            pre = cur;
            if (cur * i <= 23333333) {
                recursion(count + 1, pre, cur * i);
            } else {
                break;
            }
        }
    }
}

2022年第十三届蓝桥杯国赛JavaB组-决赛部分题解_第3张图片

2022年第十三届蓝桥杯国赛JavaB组-决赛部分题解_第4张图片

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int n = scn.nextInt();
        int m = scn.nextInt();
        char[] ops = new char[m];
        int[] val = new int[m];
        for (int i = 0; i < m; i++) {
            ops[i] = scn.next().charAt(0);
            val[i] = scn.nextInt();
        }
        LinkedList<Integer> res = new LinkedList<Integer>();
        int[] place = new int[n];
        for (int i = 0; i < n; i++) {
            res.addLast(i + 1);
            place[i] = i;
        }
        for (int i = 0; i < m; i++) {
            int index = 0;
            //找到目标值的位置
            for (int j = 0; j < res.size(); j++) {
                if (res.get(j) == val[i]) {
                    index = j;
                }
            }
            if (ops[i] == 'L') {
                int cur = res.get(index);
                res.remove(index);
                res.addFirst(cur);

            } else {
                int cur = res.get(index);
                res.remove(index);
                res.addLast(cur);
            }
        }
        for (Integer re : res) {
            System.out.print(re + " ");
        }
    }
}

2022年第十三届蓝桥杯国赛JavaB组-决赛部分题解_第5张图片

做了两个多小时,人嘛了

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int height = scn.nextInt();
        int width = scn.nextInt();
        int n = scn.nextInt();
        String[] ops = new String[n];
        List<int[]> res = new ArrayList<int[]>();
        char[][] windowMap = new char[height][width];
        for (int i =0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                windowMap[i][j] = '.';
            }
        }
        for (int i = 0; i < n; i++) {
            ops[i] = scn.next();
            int[] arr;
            if (ops[i].equals("new")) {
                arr = new int[5];
                for (int j= 0; j < 5; j++) {
                    arr[j] = scn.nextInt();
                }
            } else if (ops[i].equals("resize")){
                arr = new int[3];
                for (int j= 0; j < 3; j++) {
                    arr[j] = scn.nextInt();
                }
            }  else if (ops[i].equals("move")){
                arr = new int[3];
                for (int j= 0; j < 3; j++) {
                    arr[j] = scn.nextInt();
                }
            }  else if (ops[i].equals("close")){
                arr = new int[1];
                arr[0] = scn.nextInt();
            } else {
                //active
                arr = new int[1];
                arr[0] = scn.nextInt();
            }
            res.add(arr);
        }
        //存储权重
        Map<Integer, Integer> tops = new HashMap<Integer, Integer>();
        //存储窗口
        Map<Integer, int[]> windows = new HashMap<Integer, int[]>();
        int weight = 0;
        for (int i = 0; i < n; i++) {
            weight++;
            int[] arr = res.get(i);
            int pid = arr[0];
            if (ops[i].equals("new")) {
                int top = arr[1];
                int left = arr[2];
                int hgt = arr[3];
                int wth = arr[4];
                //存储窗口
                windows.put(pid, new int[]{top, left, hgt, wth});
                //权重
                tops.put(pid, weight);
            } else if (ops[i].equals("resize")){
                int hgt = arr[1];
                int wth = arr[2];
                int[] windowVal = windows.getOrDefault(pid, new int[]{0,0,0,0});
                int top = windowVal[0];
                int left = windowVal[1];
                windows.put(pid, new int[]{top, left, hgt, wth});
                tops.put(pid, weight);
            }  else if (ops[i].equals("move")){
                int vertical = arr[1];
                int horizontal = arr[2];
                int[] windowVal = windows.getOrDefault(pid, new int[]{0,0,0,0});
                int top = windowVal[0];
                int left = windowVal[1];
                int hgt = windowVal[2];
                int wth = windowVal[3];
                windows.put(pid, new int[]{top + vertical, left + horizontal, hgt, wth});
                tops.put(pid, weight);
            }  else if (ops[i].equals("close")){
                windows.remove(pid);
                tops.remove(pid);
            } else{
                //active
                tops.put(pid, weight);
            }
        }
        //建立数据根据权重排序id
        List<Map.Entry<Integer, Integer>> sortArr = new ArrayList<Map.Entry<Integer, Integer>>();

        //打印
        for (Map.Entry<Integer, Integer> entry : tops.entrySet()) {
            sortArr.add(entry);
        }
        //SORT
        Collections.sort(sortArr, new Comparator<Map.Entry<Integer, Integer>>() {
            @Override
            public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
                return o1.getValue() - o2.getValue();
            }
        });

        //进行绘制
        for (Map.Entry<Integer, Integer> entry : sortArr) {
            int pid = entry.getKey();
            int[] window = windows.get(pid);
            int top = window[0];
            int left = window[1];
            int hgt = window[2];
            int wth = window[3];
            //改变数组
            //四个点
            int[][] jiaos = new int[4][2];
            //上左,下左,上右,下右, 先X,后Y
            jiaos[0][0] = left;
            jiaos[0][1] = top;
            jiaos[1][0] = left;
            jiaos[1][1] = top + hgt - 1;
            jiaos[2][0] = left + wth - 1;
            jiaos[2][1] = top;
            jiaos[3][0] = left + wth - 1;
            jiaos[3][1] = top + hgt - 1;
            for (int i = 0; i < 4; i++) {
                //确定每超出边界
                if (jiaos[i][0] >= 0 && jiaos[i][0] < width && jiaos[i][1] >=0 && jiaos[i][1] < height) {
                    windowMap[jiaos[i][1]][jiaos[i][0]] = '+';
                }

            }
            //边
            for (int i = left + 1; i < left + wth - 1; i++) {
                if ( i >= 0 && i < width) {
                    if (jiaos[0][1] >=0 && jiaos[0][1] < height) {
                        windowMap[jiaos[0][1]][i] = '-';
                    }
                    if (jiaos[1][1] >=0 && jiaos[1][1] < height) {
                        windowMap[jiaos[1][1]][i] = '-';
                    }
                }
            }
            for (int i = top + 1; i <  top + hgt - 1; i++) {
                if ( i >= 0 && i < height) {
                    if (jiaos[0][0] >=0 && jiaos[0][0] < width) {
                        windowMap[i][jiaos[0][0]] = '|';
                    }
                    if (jiaos[2][0] >=0 && jiaos[2][0] < width) {
                        windowMap[i][jiaos[2][0]] = '|';
                    }
                }
            }
            //里面
            for (int i = top + 1; i < top + hgt - 1; i++) {
                for (int j = left + 1; j < left + wth - 1; j++) {
                    if (i >= 0 && i < height && j >= 0 && j < width) {
                        windowMap[i][j] = ' ';
                    }
                }
            }
        }
        for (char[] chars : windowMap) {
            for (char c : chars) {
                System.out.print(c);
            }
            System.out.println();
        }

    }
}

2022年第十三届蓝桥杯国赛JavaB组-决赛部分题解_第6张图片

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;
public class Main {
    static int n;
    static int[][] trans;
    static int[][] direction = new int[][]{{1,0}, {-1, 0}, {0, -1}, {0, 1}};
    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        n = scn.nextInt();
        int m = scn.nextInt();
        trans = new int[n][n];
        int total = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                trans[i][j] = -1;
            }
        }
        for (int i = 0; i < m; i++) {
            int x1 = scn.nextInt() - 1;
            int y1 = scn.nextInt() - 1;
            int x2 = scn.nextInt() - 1;
            int y2 = scn.nextInt() - 1;
            trans[y1][x1] = y2 *n + x2;
            trans[y2][x2] = y1 *n + x1;
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                Boolean[][] mapFlag = new Boolean[n][n];
                for (int q = 0; q < n; q++) {
                    for (int k = 0; k < n; k++) {
                        mapFlag[q][k] = false;
                    }
                }
                Boolean flag = false;
                Integer count = 0;
                Queue<int[]> queue = new ArrayDeque<int[]>();

                queue.add(new int[]{j, i, 0});
                while (!flag) {
                    int[] poll = queue.poll();
                    int x = poll[0];
                    int y = poll[1];
                    int step = poll[2] + 1;
                    if (x == n - 1 && y == n - 1) {
                        count = step - 1;
                        flag = true;
                    }
                    if (flag) {
                        break;
                    }
                    if (mapFlag[y][x]) {
                        continue;
                    }
                    mapFlag[y][x] = true;
                    if (trans[y][x] != -1) {
                        int nx = trans[y][x] % n;
                        int ny = trans[y][x] / n;
                        if (!mapFlag[ny][nx]) {
                            queue.add(new int[]{nx, ny, step});
                        }
                    }
                    for (int[] dir : direction) {
                        int nx = x + dir[0];
                        int ny = y + dir[1];
                        if (nx >=0 && nx < n && ny >=0 && ny < n && !mapFlag[ny][nx]) {
                            queue.add(new int[]{nx, ny, step});
                        }
                    }
                }
                total += count;
            }
        }
        System.out.println(String.format("%.2f", (float)total / (float) (n * n)));
    }
}

2022年第十三届蓝桥杯国赛JavaB组-决赛部分题解_第7张图片
2022年第十三届蓝桥杯国赛JavaB组-决赛部分题解_第8张图片

你可能感兴趣的:(算法,蓝桥杯,java,算法)