第十四届蓝桥杯模拟赛(第三期)Java组个人题解
创始人
2024-06-01 01:23:07
0

第十四届蓝桥杯模拟赛(第三期)Java组个人题解

今天做了一下第三期的校内模拟赛,有些地方不确定,欢迎讨论和指正~


文章目录

  • 第十四届蓝桥杯模拟赛(第三期)Java组个人题解
  • 填空题部分
    • 第一题【最小数】
    • 第二题【Excel的列】
    • 第三题【日期数】
    • 第四题【取数】
    • 第五题【最大连通块】
  • 编程题部分
    • 第六题【一周第几天】
    • 第七题【被覆盖的点】
    • 第八题【未被清理的区域】
    • 第九题【滑行距离】
    • 第十题【可重复贡献度问题】
  • 结语


填空题部分

第一题【最小数】

请找到一个大于 2022 的最小数,这个数转换成十六进制之后,所有的数位(不含前导 0)都为字母(A 到 F)。请将这个数的十进制形式作为答案提交。
答案: 2730

思路: 使用Integer.toString()方法快速转换为16进制,然后暴力每个字符,找到第一个就结束。

题解:

public class Main{public static void main(String[] args) {//2730int num = 2023;while (true){String string = Integer.toString(num, 16);char[] chars = string.toCharArray();boolean flag = false;for (char aChar : chars) {if (aChar < 'a' || aChar > 'f') {flag = true;//标记不合法break;}}if (!flag){//如果合法System.out.println(num);break;}num++;}}
}

第二题【Excel的列】

在 Excel 中,列的名称使用英文字母的组合。前 26 列用一个字母,依次为 A 到 Z,接下来 26*26 列使用两个字母的组合,依次为 AA 到 ZZ。请问第 2022 列的名称是什么?
答案: BYT

思路: 先排除掉长度为1和2的字母,从AAA开始遍历,个数达到2022即可输出结果。

题解:

public class Main{public static void main(String[] args) {int count = 26 + 26 * 26;for (int i = 'A'; i <= 'Z'; i++) {for (int j = 'A'; j <= 'Z'; j++) {for (int k = 'A'; k <= 'Z'; k++) {count++;if (count == 2022) {System.out.println((char) i + "" + (char) j + "" + (char) k);}}}}}
}

第三题【日期数】

对于一个日期,我们可以计算出年份的各个数位上的数字之和,也可以分别计算月和日的各位数字之和。请问从 1900 年 1 月 1 日至 9999 年 12 月 31 日,总共有多少天,年份的数位数字之和等于月的数位数字之和加日的数位数字之和。
  例如,2022年11月13日满足要求,因为 2+0+2+2=(1+1)+(1+3) 。
  请提交满足条件的日期的总数量。

答案: 70910

思路: 本来想用日历类的,但是不太熟悉,所以直接枚举吧。注意一下闰年和大月即可。

题解:

public class Main{static int count;public static void main(String[] args) {//70910for (int year = 1900; year <= 9999; year++) {for (int month = 1; month <= 12; month++) {if (month == 2 && judgeYear(year)) {for (int day = 1; day <= 29; day++) {if (judge(year, month, day)) {count++;}}} else if (month == 2 && !judgeYear(year)) {for (int day = 1; day <= 28; day++) {if (judge(year, month, day)) {count++;}}} else if (judgeBigMonth(month)) {for (int day = 1; day <= 31; day++) {if (judge(year, month, day)) {count++;}}} else if (!judgeBigMonth(month)) {for (int day = 1; day <= 30; day++) {if (judge(year, month, day)) {count++;}}}}}System.out.println(count);}public static boolean judgeYear(int year) {return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;}public static boolean judgeBigMonth(int month) {return month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12;}public static boolean judge(int year, int month, int day) {int sumYear = 0;int sumMonth = 0;int sumDay = 0;while (year != 0) {sumYear += year % 10;year /= 10;}while (month != 0) {sumMonth += month % 10;month /= 10;}while (day != 0) {sumDay += day % 10;day /= 10;}return sumYear == sumMonth + sumDay;}
}

第四题【取数】

小蓝有 30 个数,分别为:99, 22, 51, 63, 72, 61, 20, 88, 40, 21, 63, 30, 11, 18, 99, 12, 93, 16, 7, 53, 64, 9, 28, 84, 34, 96, 52, 82, 51, 77 。
  小蓝可以在这些数中取出两个序号不同的数,共有 30*29/2=435 种取法。
  请问这 435 种取法中,有多少种取法取出的两个数的乘积大于等于 2022 。

答案: 189

思路: 由于序号不同,导致拿出的方法也不同,和题意一样,同样需要考虑重复拿取的问题。

题解:

public class Main{static int count;public static void main(String[] args) {int[] arr = {99, 22, 51, 63, 72, 61, 20, 88, 40, 21, 63, 30, 11, 18, 99, 12, 93, 16, 7, 53, 64, 9, 28, 84, 34, 96, 52, 82, 51, 77};for (int i = 0; i < arr.length; i++) {for (int j = 0; j < arr.length; j++) {if (i != j) {if (arr[i] * arr[j] >= 2022) {count++;}}}}System.out.println(count / 2);}
}

第五题【最大连通块】

如果从一个标为 1 的位置可以通过上下左右走到另一个标为 1 的位置,则称两个位置连通。与某一个标为 1 的位置连通的所有位置(包括自己)组成一个连通分块。
  请问矩阵中最大的连通分块有多大?

答案: 148

思路: dfs和bfs都可以,这里使用的bfs,注意一点:“包括自己”,所以计算连通的个数的时候从1开始。

题解:

import java.util.ArrayDeque;
import java.util.Deque;public class Main{static String[] matrix = new String[30];static char[][] map = new char[30][60];static int[][] dis = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};static int max = 1;public static void main(String[] args) {matrix = new String[]{"110010000011111110101001001001101010111011011011101001111110","010000000001010001101100000010010110001111100010101100011110","001011101000100011111111111010000010010101010111001000010100","101100001101011101101011011001000110111111010000000110110000","010101100100010000111000100111100110001110111101010011001011","010011011010011110111101111001001001010111110001101000100011","101001011000110100001101011000000110110110100100110111101011","101111000000101000111001100010110000100110001001000101011001","001110111010001011110000001111100001010101001110011010101110","001010101000110001011111001010111111100110000011011111101010","011111100011001110100101001011110011000101011000100111001011","011010001101011110011011111010111110010100101000110111010110","001110000111100100101110001011101010001100010111110111011011","111100001000001100010110101100111001001111100100110000001101","001110010000000111011110000011000010101000111000000110101101","100100011101011111001101001010011111110010111101000010000111","110010100110101100001101111101010011000110101100000110001010","110101101100001110000100010001001010100010110100100001000011","100100000100001101010101001101000101101000000101111110001010","101101011010101000111110110000110100000010011111111100110010","101111000100000100011000010001011111001010010001010110001010","001010001110101010000100010011101001010101101101010111100101","001111110000101100010111111100000100101010000001011101100001","101011110010000010010110000100001010011111100011011000110010","011110010100011101100101111101000001011100001011010001110011","000101000101000010010010110111000010101111001101100110011100","100011100110011111000110011001111100001110110111001001000111","111011000110001000110111011001011110010010010110101000011111","011110011110110110011011001011010000100100101010110000010011","010011110011100101010101111010001001001111101111101110011101"};for (int i = 0; i < 30; i++) {map[i] = matrix[i].toCharArray();}for (int i = 0; i < map.length; i++) {for (int j = 0; j < map[i].length; j++) {if (map[i][j] == '1')bfs(new Node(i, j));}}System.out.println(max);//148}private static void bfs(Node node) {int sum = 1;boolean[][] vis = new boolean[30][60];Deque deque = new ArrayDeque<>();deque.addLast(node);vis[node.x][node.y] = true;while (!deque.isEmpty()) {Node poll = deque.pollFirst();for (int i = 0; i < 4; i++) {int dx = poll.x + dis[i][0];int dy = poll.y + dis[i][1];if (dx >= 0 && dx < 30 && dy >= 0 && dy < 60 && map[dx][dy] == '1' && !vis[dx][dy]) {vis[dx][dy] = true;deque.addLast(new Node(dx, dy));if (++sum > max) {max = sum;}}}}}static class Node {int x;int y;public Node(int x, int y) {this.x = x;this.y = y;}}
}

编程题部分

第六题【一周第几天】

问题描述
  给定一天是一周中的哪天,请问 n 天后是一周中的哪天?
输入格式
  输入第一行包含一个整数 w,表示给定的天是一周中的哪天,w 为 1 到 6 分别表示周一到周六,w 为 7 表示周日。
  第二行包含一个整数 n。
输出格式
  输出一行包含一个整数,表示 n 天后是一周中的哪天,1 到 6 分别表示周一到周六,7 表示周日。
样例输入
6
10
样例输出
2

思路: 相加取余即可,注意余数为0的时候,是周日,需要手动转换一下。

题解:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;public class Main{static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));public static void main(String[] args) throws IOException {int w = Integer.parseInt(br.readLine());int n = Integer.parseInt(br.readLine());int day = (w + n) % 7;System.out.println(day == 0 ? 7 : day);}
}

第七题【被覆盖的点】

问题描述
  小蓝负责一块区域的信号塔安装,整块区域是一个长方形区域,建立坐标轴后,西南角坐标为 (0, 0), 东南角坐标为 (W, 0), 西北角坐标为 (0, H), 东北角坐标为 (W, H)。其中 W, H 都是整数。
  他在 n 个位置设置了信号塔,每个信号塔可以覆盖以自己为圆心,半径为 R 的圆形(包括边缘)。
  为了对信号覆盖的情况进行检查,小蓝打算在区域内的所有横纵坐标为整数的点进行测试,检查信号状态。其中横坐标范围为 0 到 W,纵坐标范围为 0 到 H,总共测试 (W+1) * (H+1) 个点。
  给定信号塔的位置,请问这 (W+1)*(H+1) 个点中有多少个点被信号覆盖。
输入格式
  输入第一行包含四个整数 W, H, n, R,相邻整数之间使用一个空格分隔。
  接下来 n 行,每行包含两个整数 x, y,表示一个信号塔的坐标。信号塔可能重合,表示两个信号发射器装在了同一个位置。
输出格式
  输出一行包含一个整数,表示答案。
样例输入
10 10 2 5
0 0
7 0
样例输出
57
评测用例规模与约定
  对于所有评测用例,1 <= W, H <= 100,1 <= n <= 100, 1 <= R <= 100, 0 <= x <= W, 0 <= y <= H。

思路: 由于用例规模比较小,使用普通BFS求解即可

题解:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Deque;public class Main{static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));static boolean[][] vis;static int w, h, n, r, count;static int[][] dis = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};//宽W,高Hpublic static void main(String[] args) throws IOException {String[] split = br.readLine().split(" ");w = Integer.parseInt(split[0]);h = Integer.parseInt(split[1]);n = Integer.parseInt(split[2]);r = Integer.parseInt(split[3]);vis = new boolean[h + 1][w + 1];for (int i = 0; i < n; i++) {String[] temp = br.readLine().split(" ");bfs(new Node(Integer.parseInt(temp[0]), Integer.parseInt(temp[1])));}System.out.println(count);}private static void bfs(Node node) {Deque deque = new ArrayDeque<>();deque.addLast(node);while (!deque.isEmpty()) {Node poll = deque.pollFirst();for (int i = 0; i < 4; i++) {int dx = poll.x + dis[i][0];int dy = poll.y + dis[i][1];if (dx >= 0 && dx <= w && dy >= 0 && dy <= h && dis(node.x, node.y, dx, dy) <= r && !vis[dx][dy]) {vis[dx][dy] = true;deque.addLast(new Node(dx, dy));count++;}}}}static double dis(int x1, int y1, int x2, int y2) {int dis = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);return Math.sqrt(dis);}static class Node {int x;int y;public Node(int x, int y) {this.x = x;this.y = y;}}
}

第八题【未被清理的区域】

问题描述
  小蓝有一个 n * m 大小的矩形水域,小蓝将这个水域划分为 n 行 m 列,行数从 1 到 n 标号,列数从 1 到 m 标号。每行和每列的宽度都是单位 1 。
  现在,这个水域长满了水草,小蓝要清理水草。
  每次,小蓝可以清理一块矩形的区域,从第 r1 行(含)到第 r2 行(含)的第 c1 列(含)到 c2 列(含)。
  经过一段时间清理后,请问还有多少地方没有被清理过。
输入格式
  输入第一行包含两个整数 n, m,用一个空格分隔。
  第二行包含一个整数 t ,表示清理的次数。
  接下来 t 行,每行四个整数 r1, c1, r2, c2,相邻整数之间用一个空格分隔,表示一次清理。请注意输入的顺序。
输出格式
  输出一行包含一个整数,表示没有被清理过的面积。
样例输入
2 3
2
1 1 1 3
1 2 2 2
样例输出
2
样例输入
30 20
2
5 5 10 15
6 7 15 9
样例输出
519
评测用例规模与约定
  对于所有评测用例,1 <= r1 <= r2 <= n <= 100, 1 <= c1 <= c2 <= m <= 100, 0 <= t <= 100。

思路: 简单题,通过给数组赋值,使用0或1来表示清理与未清理即可,推荐使用Arrays.fill()方法来填充数组。

题解:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;public class Main{static int n, m, t;static int[][] map;static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));public static void main(String[] args) throws IOException {String[] first = br.readLine().split(" ");n = Integer.parseInt(first[0]);m = Integer.parseInt(first[1]);map = new int[n][m];t = Integer.parseInt(br.readLine());for (int i = 0; i < t; i++) {String[] temp = br.readLine().split(" ");int r1 = Integer.parseInt(temp[0]);int c1 = Integer.parseInt(temp[1]);int r2 = Integer.parseInt(temp[2]);int c2 = Integer.parseInt(temp[3]);for (int j = r1 - 1; j <= r2 - 1; j++) {Arrays.fill(map[j], c1 - 1, c2, 1);}}int count = 0;for (int[] ints : map) {for (int anInt : ints) {if (anInt == 0) {count++;}}}System.out.println(count);}
}

第九题【滑行距离】

问题描述
  小蓝准备在一个空旷的场地里面滑行,这个场地的高度不一,小蓝用一个 n 行 m 列的矩阵来表示场地,矩阵中的数值表示场地的高度。
  如果小蓝在某个位置,而他上、下、左、右中有一个位置的高度(严格)低于当前的高度,小蓝就可以滑过去,滑动距离为 1 。
  如果小蓝在某个位置,而他上、下、左、右中所有位置的高度都大于等于当前的高度,小蓝的滑行就结束了。
  小蓝不能滑出矩阵所表示的场地。
  小蓝可以任意选择一个位置开始滑行,请问小蓝最多能滑行多远距离。
输入格式
  输入第一行包含两个整数 n, m,用一个空格分隔。
  接下来 n 行,每行包含 m 个整数,相邻整数之间用一个空格分隔,依次表示每个位置的高度。
输出格式
  输出一行包含一个整数,表示答案。
样例输入
4 5
1 4 6 3 1
11 8 7 3 1
9 4 5 2 1
1 3 2 2 1
样例输出
7
样例说明
  滑行的位置一次为 (2, 1), (2, 2), (2, 3), (3, 3), (3, 2), (4, 2), (4, 3)。
评测用例规模与约定
  对于 30% 评测用例,1 <= n <= 20,1 <= m <= 20,0 <= 高度 <= 100。
  对于所有评测用例,1 <= n <= 100,1 <= m <= 100,0 <= 高度 <= 10000。

思路: 普通BFS即可求解,注意一点:走不动的区域要回退。

题解:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Deque;public class Main{static int n, m;static int max;static int[][] map;static int[][] dis = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));public static void main(String[] args) throws IOException {String[] split = br.readLine().split(" ");n = Integer.parseInt(split[0]);m = Integer.parseInt(split[1]);map = new int[n][m];for (int i = 0; i < n; i++) {String[] temp = br.readLine().split(" ");for (int j = 0; j < temp.length; j++) {map[i][j] = Integer.parseInt(temp[j]);}}for (int i = 0; i < map.length; i++) {for (int j = 0; j < map[i].length; j++) {bfs(new Node(i, j));}}System.out.println(max);}private static void bfs(Node node) {int count = 0;boolean[][] vis = new boolean[n][m];Deque deque = new ArrayDeque<>();deque.addLast(node);vis[node.x][node.y] = true;while (!deque.isEmpty()) {Node poll = deque.pollFirst();for (int i = 0; i < 4; i++) {int dx = poll.x + dis[i][0];int dy = poll.y + dis[i][1];if (dx >= 0 && dx < n && dy >= 0 && dy < m && !vis[dx][dy]) {vis[dx][dy] = true;if (judgePass(dx, dy)) {deque.addLast(new Node(dx, dy));count++;}elsecount--;}}}if (max < count) max = count;}static boolean judgePass(int x, int y) {boolean flag = false;if (x - 1 >= 0 && y >= 0 && x - 1 < n && y < m) {if (map[x - 1][y] < map[x][y])flag = true;}if (x + 1 >= 0 && y >= 0 && x + 1 < n && y < m) {if (map[x + 1][y] < map[x][y]) {flag = true;}}if (x >= 0 && y - 1 >= 0 && x < n && y - 1 < m) {if (map[x][y - 1] < map[x][y]) {flag = true;}}if (x >= 0 && y + 1 >= 0 && x < n && y + 1 < m) {if (map[x][y + 1] < map[x][y]) {flag = true;}}return flag;}static class Node {int x;int y;public Node(int x, int y) {this.x = x;this.y = y;}}
}

第十题【可重复贡献度问题】

问题描述
  问题描述:小蓝有一个序列 a[1], a[2], …, a[n]。
  给定一个正整数 k,请问对于每一个 1 到 n 之间的序号 i,a[i-k], a[i-k+1], …, a[i+k] 这 2k+1 个数中的最小值是多少?当某个下标超过 1 到 n 的范围时,数不存在,求最小值时只取存在的那些值。
输入格式
  输入的第一行包含一整数 n。
  第二行包含 n 个整数,分别表示 a[1], a[2], …, a[n]。
  第三行包含一个整数 k 。
输出格式
  输出一行,包含 n 个整数,分别表示对于每个序号求得的最小值。
样例输入
5
5 2 7 4 3
1
样例输出
2 2 2 3 3
评测用例规模与约定
  对于 30% 的评测用例,1 <= n <= 1000,1 <= a[i] <= 1000。
  对于 50% 的评测用例,1 <= n <= 10000,1 <= a[i] <= 10000。
  对于所有评测用例,1 <= n <= 1000000,1 <= a[i] <= 1000000。

思路: 注意看样例范围,对于百万级的数据,暴力求解的时间复杂度为O(n²),必定超时,对于静态的区间最值问题考虑ST表,每次查询的时间复杂度是O(1),但是预处理的时间复杂度是O(nlogn);如果是动态问题,考虑使用线段树求解O(nlogn)。

ST表题解:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;public class Main{static int n, k, f;static int[] array;static int[][] ST;static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));public static void main(String[] args) throws IOException {String fist = br.readLine();n = Integer.parseInt(fist);f = (int) Math.ceil(Math.log(n) / Math.log(2));array = new int[n];ST = new int[n][f];String[] second = br.readLine().split(" ");for (int i = 0; i < n; i++) array[i] = Integer.parseInt(second[i]);k = Integer.parseInt(br.readLine());init();for (int i = 0; i < n; i++) {int begin = Math.max(i - k, 0);int end = i + k < n ? i + k : n - 1;System.out.print(query(begin, end) + " ");}}static void init() {for (int i = 0; i < n; i++) {ST[i][0] = array[i];}for (int k = 1; k < f; k++) {for (int s = 0; s + (1 << k) <= n; s++) {ST[s][k] = Math.min(ST[s][k - 1], ST[s + (1 << (k - 1))][k - 1]);}}}static int query(int begin, int end) {int len = end - begin + 1;int k = (int) (Math.log(len) / Math.log(2));return Math.min(ST[begin][k], ST[end - (1 << k) + 1][k]);}
}

结语

题解全为本人手写,无抄袭,有些不完美,也可能有小错误,请大家批评指正。

文章粗浅,希望对大家有帮助!

相关内容

热门资讯

122.(leaflet篇)l... 听老人家说:多看美女会长寿 地图之家总目录(订阅之前建议先查看该博客) 文章末尾处提供保证可运行...
育碧GDC2018程序化大世界... 1.传统手动绘制森林的问题 采用手动绘制的方法的话,每次迭代地形都要手动再绘制森林。这...
育碧GDC2018程序化大世界... 1.传统手动绘制森林的问题 采用手动绘制的方法的话,每次迭代地形都要手动再绘制森林。这...
Vue使用pdf-lib为文件... 之前也写过两篇预览pdf的,但是没有加水印,这是链接:Vu...
PyQt5数据库开发1 4.1... 文章目录 前言 步骤/方法 1 使用windows身份登录 2 启用混合登录模式 3 允许远程连接服...
Android studio ... 解决 Android studio 出现“The emulator process for AVD ...
Linux基础命令大全(上) ♥️作者:小刘在C站 ♥️个人主页:小刘主页 ♥️每天分享云计算网络运维...
再谈解决“因为文件包含病毒或潜... 前面出了一篇博文专门来解决“因为文件包含病毒或潜在的垃圾软件”的问题,其中第二种方法有...
南京邮电大学通达学院2023c... 题目展示 一.问题描述 实验题目1 定义一个学生类,其中包括如下内容: (1)私有数据成员 ①年龄 ...
PageObject 六大原则 PageObject六大原则: 1.封装服务的方法 2.不要暴露页面的细节 3.通过r...
【Linux网络编程】01:S... Socket多进程 OVERVIEWSocket多进程1.Server2.Client3.bug&...
数据结构刷题(二十五):122... 1.122. 买卖股票的最佳时机 II思路:贪心。把利润分解为每天为单位的维度,然后收...
浏览器事件循环 事件循环 浏览器的进程模型 何为进程? 程序运行需要有它自己专属的内存空间࿰...
8个免费图片/照片压缩工具帮您... 继续查看一些最好的图像压缩工具,以提升用户体验和存储空间以及网站使用支持。 无数图像压...
计算机二级Python备考(2... 目录  一、选择题 1.在Python语言中: 2.知识点 二、基本操作题 1. j...
端电压 相电压 线电压 记得刚接触矢量控制的时候,拿到板子,就赶紧去测各种波形,结...
如何使用Python检测和识别... 车牌检测与识别技术用途广泛,可以用于道路系统、无票停车场、车辆门禁等。这项技术结合了计...
带环链表详解 目录 一、什么是环形链表 二、判断是否为环形链表 2.1 具体题目 2.2 具体思路 2.3 思路的...
【C语言进阶:刨根究底字符串函... 本节重点内容: 深入理解strcpy函数的使用学会strcpy函数的模拟实现⚡strc...
Django web开发(一)... 文章目录前端开发1.快速开发网站2.标签2.1 编码2.2 title2.3 标题2.4 div和s...