【蓝桥杯】第十六届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

相关推荐

  • 思维导图xmind如何安装?附安装包

    前言 大家好,我是小徐啊。我们在Java开发中,有时候是需要用到思维导图的,这可以帮助我们更好的理清思路,提高开发的效率。而说到思维导图,最有名的就是xmind了,它的功能十分强大,几乎是思维导图里面最强大的那一个。但是,默认只能使用初级功能,高级功能需要额外再开通,今天小徐就来介绍下如何安装xmind以及升级,让我们可以使用pro的功能。文末附获取方式。 …

    2025 年 1 月 13 日
    48100
  • Intellij IDEA 永久激活破解问题汇总

    IDEA最新永久激活破解教程:https://www.it1024doc.com/4100.html 1. 输入激活码后提示:“key is invalid”(无效key),也就是激活码无效 出现上面的情况后,一般都是工具没有生效,造成激活失败,请仔细看教程,是否遗漏了什么步骤。也有小伙伴在尝试重启自己电脑后,激活成功。总之,没有激活成功问题比较多,大家可以…

    未分类 2024 年 6 月 22 日
    1.3K00
  • JAVA 图形界面编程 AWT篇(1)

    前言 为了应对JAVA课设,小编走上了java的图形界面编程的道路,通过博客分享自己的学习历程,并进行笔记的记录。 AWT(Abstract Window Toolkit)介绍 AWT(抽象窗口工具包)是 Java 最早的图形用户界面(GUI)框架之一,主要用于构建桌面应用程序的图形界面。最初在 JDK 1.0 版本中作为 Java GUI 的核心库引入,旨…

    未分类 2025 年 1 月 11 日
    45600
  • 扣子又出新功能,支持一键部署小程序,太强了!!

    大家好,我是R哥。 作为一名程序员和技术博主,我一直关注如何使用工具提升生产力,尤其是在内容创作和应用开发领域。 拿我开发一个微信小程序为例,我需要懂前端、后端、运维 等全栈技术,开发流程和技术栈复杂,我还需要购买云服务器、云数据库 等各种基础设施,资源耗费非常多。 虽然现在有如 Cursor 这样的革命性 AI 开发工具,它突破了传统开发模式的壁垒,非开发…

    2025 年 1 月 11 日
    64600
  • Java之Spring MVC篇三

    ![](https://pic.it1024doc.com/csdn/202412/1a8934ea1b097949f559a12ad79fd34c.gif)​​​​​​​ **目录** [响应](#响应) [返回静态页面](#返回静态页面) [@RestController 和 @Controller的区别和联系](#@RestController-和-@…

    2024 年 12 月 27 日
    45600

发表回复

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

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信