第 28 场 蓝桥入门赛 JAVA 完整题解

前言


本文总结了六个编程题目的解题思路与核心考点,涵盖基础语法、逻辑分析、贪心算法、数学推导等知识点。每个题目均从问题本质出发,通过巧妙的算法设计或数学优化降低复杂度,展现了不同场景下的编程思维与解题技巧。以下为各题的详细考点解析。


题目考点总结

  1. 胜利终将属于你
    • 考点:基础输出语句
    • 关键点:直接输出固定字符串,无需任何计算或逻辑处理,考察对编程语言基本语法的掌握。
  1. 团队赛
    • 考点:条件判断与逻辑分析
    • 关键点:通过严格比较三人能力值,判断是否存在队长。需注意条件覆盖的完整性(逐一检查每个成员是否满足队长条件)。
  1. 破译密码
    • 考点:贪心算法与任务调度(Johnson 法则)
    • 关键点:将任务分为两组,分别按破译时间升序和传输时间降序排序,合并后模拟处理流程以最小化总完成时间。
  1. 浓缩咖啡液
    • 考点:极值判断与数学性质
    • 关键点:混合后的浓度必在初始浓度的最小值和最大值之间,只需判断目标浓度是否在此区间内。
  1. 蓝桥运算
    • 考点:数学推导与优化计算
    • 关键点:分析区间操作对总和的贡献,避免逐个元素遍历。通过计算操作对数与奇偶性快速更新总和。
  1. 插入数字
    • 考点:字符串操作与去重处理
    • 关键点:枚举所有插入位置和数字,利用集合去重,同时处理前导零问题(首位不能插入 0)。

通过以上题目,可全面锻炼基础编程能力、算法设计思维及数学建模技巧,尤其注重对问题本质的洞察与高效实现。

完整Java代码

1. 胜利终将属于你

public class Main {
    public static void main(String[] args) {
        System.out.println("I will fight and win");
    }
}

2. 团队赛

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        int c = scanner.nextInt();
        
        if (a > b + c) {
            System.out.println("l");
        } else if (b > a + c) {
            System.out.println("q");
        } else if (c > a + b) {
            System.out.println("b");
        } else {
            System.out.println(-1);
        }
    }
}

3. 破译密码

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] A = new int[n];
        int[] B = new int[n];
        for (int i = 0; i < n; i++) A[i] = scanner.nextInt();
        for (int i = 0; i < n; i++) B[i] = scanner.nextInt();

        List<Task> groupA = new ArrayList<>();
        List<Task> groupB = new ArrayList<>();

        // 分组:A_i <= B_i的为组A,其余为组B
        for (int i = 0; i < n; i++) {
            if (A[i] <= B[i]) {
                groupA.add(new Task(A[i], B[i]));
            } else {
                groupB.add(new Task(A[i], B[i]));
            }
        }

        // 组A按破译时间升序,组B按传输时间降序
        Collections.sort(groupA, Comparator.comparingInt(t -> t.a));
        Collections.sort(groupB, (t1, t2) -> t2.b - t1.b);

        // 合并两组
        List<Task> merged = new ArrayList<>();
        merged.addAll(groupA);
        merged.addAll(groupB);

        // 计算总时间
        int currentTime = 0;
        int transmitEnd = 0;
        for (Task task : merged) {
            currentTime += task.a; // 破译时间累加
            int start = Math.max(currentTime, transmitEnd); // 传输开始时间取最大值
            transmitEnd = start + task.b; // 更新传输结束时间
        }

        System.out.println(Math.max(currentTime, transmitEnd));
    }

    static class Task {
        int a, b;
        Task(int a, int b) {
            this.a = a;
            this.b = b;
        }
    }
}

4. 浓缩咖啡液

import java.util.*;

public class CoffeeConcentration {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int T = scanner.nextInt();
        while (T-- > 0) {
            int n = scanner.nextInt();
            int m = scanner.nextInt();
            int[] A = new int[n];
            for (int i = 0; i < n; i++) A[i] = scanner.nextInt();
            Arrays.sort(A);
            // 目标浓度在最小和最大之间即可
            System.out.println(A[0] <= m && m <= A[n - 1] ? "YES" : "NO");
        }
    }
}

5. 蓝桥运算

import java.util.Scanner;

public class LanqiaoOperation {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int q = scanner.nextInt();
        long sum = 0;
        // 初始数组和
        for (int i = 0; i < n; i++) sum += scanner.nextInt();
        // 处理每次操作
        while (q-- > 0) {
            int L = scanner.nextInt();
            int R = scanner.nextInt();
            int len = R - L + 1;
            int pairs = len / 2;
            sum -= pairs; // 每对贡献-1
            if (len % 2 == 1) sum += R; // 奇数长度的最后一个元素贡献+R
        }
        System.out.println(sum);
    }
}

6. 插入数字

import java.util.*;

public class InsertNumber {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String n = scanner.next();
        Set<String> set = new HashSet<>();

        for (int i = 0; i <= n.length(); i++) {
            char start = (i == 0) ? '1' : '0'; // 开头不能为0
            for (char c = start; c <= '9'; c++) {
                String newNum = n.substring(0, i) + c + n.substring(i);
                set.add(newNum);
            }
        }

        System.out.println(set.size()); // 自动去重后的数量
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时雨h

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值