【蓝桥杯】第十六届Java B组竞赛解题实录

题目A:高塔脱险

高塔题目示意图

基础难度题目,主要考察基本编程能力,但需特别注意数值范围问题,使用int类型可能导致数据溢出
正确答案:202

package competition.t1;
import java.io.*;
public class Solution {
static class FastIO {
static InputStreamReader reader = new InputStreamReader(System.in);
static StreamTokenizer tokenizer = new StreamTokenizer(reader);
static BufferedReader buffer = new BufferedReader(reader);
static PrintWriter writer = new PrintWriter(System.out);
static int getInt() throws IOException {
tokenizer.nextToken();
return (int) tokenizer.nval;
}
}
public static void main(String[] args) {
long target = 2025;
long result = 0;
for (long i = 1; i

题目本质是寻找满足特定条件的最小N值。设两个日期为a和b,需满足:
(N + a)能被b整除
(N + b)能被a整除
虽然可以考虑中国剩余定理,但实际可通过枚举法解决
转换思路:若(N+a)是b的倍数,则可表示为N = k*b - a
通过求解k值即可推导出最终结果
答案:409876661809331

package competition.t2;
import java.io.*;
public class Solution {
static class FastIO {
// 输入输出工具类同上
}
public static void main(String[] args) {
long date1 = 20250412, date2 = 20240413;
boolean found = false;
for (int i = 9999; i

位运算异或操作解析:将数字转为二进制后,按位比较,相同为0,不同为1
解题思路有两种:
1. 统计每位1的个数,若所有位都是偶数则满足条件
2. 更简便的方法:若两组异或结果相同,则再次异或必为0
因此只需验证最终异或结果是否为0即可

public class Solution {
static class FastIO {
// 输入输出工具类同上
}
public static void main(String[] args) throws IOException {
int testCases = FastIO.getInt();
while (testCases-- > 0) {
int count = FastIO.getInt();
int xorResult = 0;
for (int i = 1; i

题目要求计算a数组和b数组元素两两组合,其和为质数且不超过n+m的组合数量
关键点在于高效质数判断,直接暴力检测会超时
解决方案:使用埃拉托斯特尼筛法预处理质数表

public class Solution {
static class FastIO {
// 输入输出工具类同上
}
static Set primeCache = new TreeSet<>();
static {
// 预处理质数表
int maxNum = 400000;
boolean[] isPrime = new boolean[maxNum + 1];
Arrays.fill(isPrime, true);
for (int i = 2; i  resultSet = new HashSet<>();
for (int i = 0; i  total) break;
if (primeCache.contains(sum)) {
resultSet.add(sum);
}
}
}
FastIO.writer.print(resultSet.size());
FastIO.writer.flush();
}
}
}

题目E:爆破装置

爆破题目示意图

题目转化为图论问题:将所有圆形装置连接成最小生成树
解题步骤:
1. 若两圆相交,边权为0
2. 若不相交,边权为圆心距减去两圆半径
3. 应用Kruskal或Prim算法求解

public class Solution {
static class FastIO {
// 输入输出工具类同上
}
static class Circle {
int id;
int x;
int y;
int radius;
public Circle(int id, int x, int y, int radius) {
this.id = id;
this.x = x;
this.y = y;
this.radius = radius;
}
}
static class Connection implements Comparable {
int from;
int to;
double cost;
public Connection(int from, int to, double cost) {
this.from = from;
this.to = to;
this.cost = cost;
}
@Override
public int compareTo(Connection other) {
return Double.compare(this.cost, other.cost);
}
}
static int[] parent;
static int findRoot(int x) {
return parent[x] == x ? x : (parent[x] = findRoot(parent[x]));
}
public static void main(String[] args) throws IOException {
int n = FastIO.getInt();
parent = new int[n + 1];
for (int i = 1; i

(注:此处省略部分题目解析,保持原文核心内容但采用不同表达方式)

题目H:研发资源调配

资源分配题目示意图

采用直接模拟的方法解决该问题

文章整理自互联网,只做测试使用。发布者:Lomu,转转请注明出处:https://www.it1024doc.com/9364.html

(0)
LomuLomu
上一篇 2025 年 5 月 13 日 上午4:19
下一篇 2025 年 5 月 13 日 上午5:00

相关推荐

  • 为什么在 Python 中 hash(-1) == hash(-2)?

    英文:https://omairmajid.com/posts/2021-07-16-why-is-hash-in-python 作者:Omair Majid 译者:豌豆花下猫&Claude-3.5-Sonnet 时间:原文发布于 2021.07.16,翻译于 2025.01.11 收录于:Python为什么系列 https://github.com/chi…

    未分类 2025 年 1 月 16 日
    40600
  • 《重构:改善既有代码的设计(第2版)》PDF、EPUB免费下载

    电子版仅供预览,下载后24小时内务必删除,支持正版,喜欢的请购买正版书籍 点击原文去下载 书籍信息 作者: [美] Martin Fowler出版社: 人民邮电出版社出品方: 异步图书副标题: 改善既有代码的设计原作名: Refactoring: Improving the Design of Existing Code,Second Edition译者: …

    2025 年 1 月 10 日
    55300
  • 【C++】深入解析explicit关键字的奥秘(从原理到实践全面掌握explicit的用法)

    目录导航一、开篇引言二、揭开explicit的神秘面纱三、构造函数的隐式转换机制🍏单参数构造函数的隐式转换🔍explicit关键字的引入契机🍊多参数构造函数的特殊情况🔍explicit的实际应用价值🔍explicit的正确使用姿势四、核心要点回顾五、学习寄语 一、开篇引言 在日常C++编程实践中,explicit关键字可能并不常见于我们的代码中。然而,在标准…

    2025 年 5 月 15 日
    33800
  • Microi 吾码与 JavaScript:前端低代码平台的强大组合

    目录 一、引言 二、Microi 吾码概述 三、JavaScript 在 Microi 吾码前端开发中的应用 (一)前端 V8 引擎与 JavaScript (二)接口引擎与 JavaScript 四、JavaScript 在 Microi 吾码后端开发中的协同 (一)与 C# 后端框架的交互 (二)利用 gRPC 实现跨语言通信 五、Microi 吾码中 …

    2025 年 1 月 10 日
    50100
  • 促销系统:促销活动、优惠券、优惠规则概念模型设计

    大家好,我是汤师爷~ 概念模型设计是促销系统开发的关键环节,我们需要基于之前的功能分析,将复杂的促销业务拆解成清晰的领域概念,这些概念之间的关系界定和边界划分,将直接决定系统的可维护性和扩展性。 促销系统核心概念模型 通过对促销业务的分析,我们可以抽象出促销系统的关键概念模型。 1、促销活动模型 促销活动模型对活动的各个要素和规则进行抽象,包含活动名称、描述…

    2025 年 1 月 6 日
    61000

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信