【蓝桥杯研究生组】第15届Java试题答案整理


题目链接 : https://www.lanqiao.cn/courses/2786/learning/?id=2870422&compatibility=false


A题


在这里插入图片描述

import java.io.*;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class Tiankong {

    public static int getKTime() throws FileNotFoundException {
        // 打开 log.txt 文件
        String filePath = "/Users/wanqingliu/Desktop/Projects/Java/lanqiao/src/log.txt";
        List<Timestamp> list = new ArrayList<>();
        int max = 0;
        // 按行读取文件并处理
        try (BufferedReader br  = new BufferedReader(new FileReader(filePath))){
            String line;
            int count = 0;
            long pre = -1;
            while ((line = br.readLine()) != null) {
                String[] s = line.split(" ");
                if (pre == -1){
                    count++;
                }
                // 如果敲击正确,并且敲击时间间隔小于 1else if (s[0].equals(s[1]) && (Long.parseLong(s[2]) - pre) <= 1000) {
                    count++;
                } else {
                    max = Math.max(max, count);
                    count = 0;
                }
                pre = Long.parseLong(s[2]);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return max;
    }

    public static int getCount() {
        long num = 2024041331404202L;
        long sum = 0;
        long multi = 1;
        int count = 0;
        for (long i = 1; i<=num; i++){
            sum = sum + i;
            multi = multi * i;
            if ((multi - sum)%100 == 0) {
                count++;
            }
        }
        return count;
    }
    
    public static void main(String[] args) {
        long num = 2024041331404202L;
        long fullCycles = num / 100; // 计算完整周期
        long remainder = num % 100; // 剩余部分
        int countPerCycle = 0; // 每个完整周期中满足条件的个数

        // 计算 1100 的周期内的结果
        long sumMod = 0;
        long multiMod = 1;

        for (long i = 1; i <= 100; i++) {
            sumMod = (sumMod + i) % 100; // 累加和的模 100
            multiMod = (multiMod * i) % 100; // 累乘的模 100

            if ((sumMod - multiMod) % 100 == 0) {
                countPerCycle++;
            }
        }

        // 计算完整周期的总结果
        long totalCount = fullCycles * countPerCycle;

        // 计算剩余部分
        sumMod = 0;
        multiMod = 1;
        for (long i = 1; i <= remainder; i++) {
            sumMod = (sumMod + i) % 100;
            multiMod = (multiMod * i) % 100;

            if ((sumMod - multiMod) % 100 == 0) {
                totalCount++;
            }
        }

        // 输出结果
        System.out.println(totalCount);
    }
}


B 题


在这里插入图片描述

答案在上题


C题


在这里插入图片描述

import java.util.*;

public class Fenbi {
    // 定义常量
    private static final List listOne = new ArrayList<Integer>();
    private static final List listTwo = new ArrayList<Integer>();

    static {
        listOne.add(0);
        listOne.add(4);
        listOne.add(6);
        listOne.add(9);
        listTwo.add(8);
    }

    public static int getFenbiCount(int num){
        int res = 0;
        while(num / 10 != 0){
            int cur = num % 10;
            if (listOne.contains(cur)){
                res += 1;
            } else if (listTwo.contains(cur)){
                res += 2;
            }
            num = num / 10;
        }
        if (listOne.contains(num)){
            res += 1;
        } else if (listTwo.contains(num)){
            res += 2;
        }
        return res;
    }


    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int count = in.nextInt();
        int[][] arr = new int[2][count];
        List<Integer> input = new ArrayList<>();
        for (int i=0; i<count; i++){
            arr[0][i] = in.nextInt();
            arr[1][i] = getFenbiCount(arr[0][i]);
        }
        // 对二维数组arr按照第2行进行排序,若第2行值相同,按照第一行进行排序
        // 将二维数组的列索引提取出来
        Integer[] indices = new Integer[arr[0].length];
        for (int i = 0; i < indices.length; i++) {
            indices[i] = i;
        }

        // 自定义排序:先按第 2 行排序,若相同按第 1 行排序
        Arrays.sort(indices, (a, b) -> {
            if (arr[1][a] != arr[1][b]) {
                return Integer.compare(arr[1][a], arr[1][b]); // 第 2 行升序
            } else {
                return Integer.compare(arr[0][a], arr[0][b]); // 第 1 行升序
            }
        });

        // 创建一个新的二维数组,按照排序后的列顺序重新构造
        int[][] sortedArr = new int[arr.length][arr[0].length];
        for (int i = 0; i < indices.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                sortedArr[j][i] = arr[j][indices[i]];
            }
        }

        // 打印排序后的数组
        System.out.println(Arrays.toString(sortedArr[0]));
    }
}


D 题


试题 D: 商品库存管理

时间限制: 3.0s 内存限制: 512.0MB 本题总分:10 分


在这里插入图片描述


【输入示例】
5 3
1 2
2 4
3 5


【输出示例】
1
0
1


import java.util.Scanner;

/*

 */
public class KuchunManage {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 商品个数
        int prouctNum = in.nextInt();
        // 操作个数
        int operationNum = in.nextInt();
        int[] products = new int[prouctNum + 1];
        int[][] operations = new int[operationNum][2];
        for (int i=0; i<operationNum; i++){
            int left = in.nextInt();
            int right = in.nextInt();
            operations[i][0] = left;
            operations[i][1] = right;
            for(int j = left; j<=right; j++) {
                products[j]++;
            }
        }
        // 去掉第 i 个操作的结果
        for (int i=0; i<operationNum; i++) {
            int left = operations[i][0];
            int right = operations[i][1];
            for(int j = left; j<=right; j++) {
                products[j]--;
            }
            int ans = 0;
            for (int k=1; k<=prouctNum; k++){
                if (products[k] == 0) {
                    ans++;
                }
            }
            // 还原
            for(int j = left; j<=right; j++) {
                products[j]++;
            }
            System.out.println(ans);
        }
    }
}

F 题


在这里插入图片描述

import java.util.Scanner;

public class HuiWen {
    // 能否加入任意个 l 、 q 、 b,使得字符串 S 变为回文字符串

    // 判断字符串 S 是否是回文字符串
    public static boolean isHuiWen(String S){
        int len = S.length();
        for (int i = 0; i<len / 2; i++){
            if (S.charAt(i) != S.charAt(len - i - 1)) {
                return false;
            }
        }
        return true;
    }

    public static boolean canBeHuiWen(String S){
        int len = S.length();
        int end = 1;
        // 从头开始早最长的回文
        for (int i = 1; i<len-1; i++) {
            String subString = S.substring(0, i);
            if (isHuiWen(subString)) {
                end = i;
            }
        }
        // 对 end 之后的字符,判断是否都为 l、q、b
        for (int i=end; i<len; i++){
            if (S.charAt(i) != 'l' && S.charAt(i) != 'q' && S.charAt(i) != 'b') {
                return false;
            }
        }
        return true;
    }

    // 判断字符串 S 中从头开始的最长的回文字串
    // 1. gmgqlq
    //  gmg
    // 剩下的 qlq 可以变为回文
    // 2. pdlbll
    // p
    // 剩下的 dlbll 不能变为回文
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int count = in.nextInt();
        for (int i=0; i<count; i++) {
            String S = in.next();
            if (isHuiWen(S)){
                System.out.println("Yes");
            } else if (canBeHuiWen(S)){
                System.out.println("Yes");
            } else {
                System.out.println("No");
            }
        }
    }
}

G题

在这里插入图片描述

import java.util.Scanner;

public class LargestYiHuo {
    public static void main(String[] args) {
        // 接收输入
        Scanner scanner = new Scanner(System.in);
        // 结点个数
        int count = scanner.nextInt();
        // 结点值
        int[] nodeNum = new int[count];
        // 结点父亲结点
        int[] nodeFather = new int[count];
        // 接收输入
        for (int i=0; i<count; i++){
            nodeNum[i] = scanner.nextInt();
        }
        for (int i=0; i<count; i++){
            nodeFather[i] = scanner.nextInt();
        }
        int max = -1;
        // 暴力搜索
        for (int i=0; i<count; i++) {
            for (int j=i+1; j<count; j++) {
                // 判断 i 和 j 是否相邻 (i 是否是 j 的父节点, 或者 j 是否是 i 的父节点)
                if (nodeFather[i] == j || nodeFather[j] == i) {
                    continue;
                } else {
                    max = Math.max(max, nodeNum[i] ^ nodeNum[j]);
                }
            }
        }
        System.out.println(max);

    }
}

H 题


在这里插入图片描述

import java.util.Scanner;

class Treenode {
    int val;
    Treenode left;
    Treenode right;

    public Treenode(int val) {
        this.val = val;
    }
}

public class ZiShu {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int nodeCount = sc.nextInt();
        Treenode[] treenodes = new Treenode[nodeCount];
        for (int i=0; i<nodeCount; i++) {
            Treenode treenode = new Treenode(sc.nextInt());
            treenodes[i] = treenode;
        }
        // 构造树结构
        for (int i=0; i<nodeCount-1; i++){
            int node1 = sc.nextInt();
            int node2 = sc.nextInt();
            node1--;
            node2--;
            if (treenodes[node1].left == null) {
                treenodes[node1].left = treenodes[node2];
            } else {
                treenodes[node1].right = treenodes[node2];
            }
        }
        int ans = 0;
        // 遍历每个子树,得到满足条件的个数
        for (int i=0; i<nodeCount; i++) {
            ans += dfs(treenodes[i], treenodes[i].val);
        }
        System.out.println(ans);
    }

    // 深度优先搜索
    public static int dfs(Treenode root, int rootVal) {
        if (root == null) {
            return 0;
        }
        int count = 0;
        if (root.val < rootVal && rootVal % root.val != 0){
            count++;
        }
        count += dfs(root.left, rootVal);
        count += dfs(root.right, rootVal);
        return count;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值