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

您所在的位置:网站首页 excel函数自动标红 第十四届蓝桥杯模拟赛(第三期)Java组个人题解

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

#第十四届蓝桥杯模拟赛(第三期)Java组个人题解| 来源: 网络整理| 查看: 265

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

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

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

填空题部分 第一题【最小数】

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

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

题解:

public class Main{ public static void main(String[] args) {//2730 int num = 2023; while (true){ String string = Integer.toString(num, 16); char[] chars = string.toCharArray(); boolean flag = false; for (char aChar : chars) { if (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 for (int k = 'A'; k 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) {//70910 for (int year = 1900; year if (month == 2 && judgeYear(year)) { for (int day = 1; day count++; } } } else if (month == 2 && !judgeYear(year)) { for (int day = 1; day count++; } } } else if (judgeBigMonth(month)) { for (int day = 1; day count++; } } } else if (!judgeBigMonth(month)) { for (int day = 1; 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 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 for (int j = 0; j 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 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 {1, 0}, {-1, 0}, {0, 1}, {0, -1}}; //宽W,高H public 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 Deque deque = new ArrayDeque(); deque.addLast(node); while (!deque.isEmpty()) { Node poll = deque.pollFirst(); for (int i = 0; i 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 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 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 {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 map[i][j] = Integer.parseInt(temp[j]); } } for (int i = 0; i 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 vis[dx][dy] = true; if (judgePass(dx, dy)) { deque.addLast(new Node(dx, dy)); count++; }else count--; } } } if (max if (map[x - 1][y] = 0 && y >= 0 && x + 1 flag = true; } } if (x >= 0 && y - 1 >= 0 && x flag = true; } } if (x >= 0 && y + 1 >= 0 && x 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 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 for (int i = 0; i for (int s = 0; s + (1 int len = end - begin + 1; int k = (int) (Math.log(len) / Math.log(2)); return Math.min(ST[begin][k], ST[end - (1



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3