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

相关推荐

  • manim边做边学–动画组合

    动画组合类的作用是将多个动画组合起来,以实现更复杂的动画效果。 Manim中有4个 用于动画组合的类: AnimationGroup:将多个动画组合在一起同时播放,能一次性呈现多个对象的不同变化 LaggedStart:按照添加顺序依次启动多个动画,每个动画间有延迟,使动画呈现出清晰的先后顺序 LaggedStartMap:对一组对象应用相同动画,并按顺序逐…

    2025 年 1 月 10 日
    37800
  • 【JavaSE】【网络协议】HTTP 请求和响应

    一、HTTP请求 1.1 请求格式 请求格式:首行+请求头(header)+空行+正文(body) 1.2 首行组成 首行组成:请求方法+URL+版本号。使用“空格”将他们分隔开。 1.2.1 请求方法 方法 说明 支持的HTTP版本 GET 获取资源 1.0 1.1 POST 传输实体主体 1.0 1.1 PUT 传输文件 1.0 1.1 DELETE 删…

    2025 年 1 月 15 日
    39600
  • PostgreSQL 初始化配置设置

    title: PostgreSQL 初始化配置设置date: 2024/12/27updated: 2024/12/27author: cmdragon excerpt:PostgreSQL是一款广泛应用于企业级应用、数据仓库以及Web应用程序的强大数据库管理系统。在完成数据库的安装后,进行合理而有效的初始配置是确保数据库性能和安全性的关键步骤。Postgr…

    2024 年 12 月 31 日
    30300
  • Java怎样实现将数据导出为Word文档

    文章首发于我的博客:Java怎样实现将数据导出为Word文档 – Liu Zijian’s Blog 我们在开发一些系统的时候,例如OA系统,经常能遇到将审批单数据导出为word和excel文档的需求,导出为excel是比较简单的,因为excel有单元格来供我们定位数据位置,但是word文档的格式不像表格那样可以轻松的定位,要想将数据导出为一些带有图片和表格…

    2025 年 1 月 12 日
    33600
  • java 8的下载安装

    java 8的下载安装 一、下载 官网下载地址:链接: https://www.oracle.com/java/technologies/downloads/#java8-windows 一般选择64位的 二、安装 下载完成双击安装即可,点击下一步 更改安装路径后点击下一步 出现该弹窗时直接×调,不需要单独安装jre,jdk已经自带jre了。 点击关闭完成安…

    2025 年 1 月 14 日
    41500

发表回复

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

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信