第十四届蓝桥杯模拟赛(第三期)——Java版

第一题

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

public class Main {
    public static void main(String[] args) {
        int i=2023;
        while (true){
            boolean flag=true;
            String s = Integer.toString(i, 16);
            for (int j = 0; j < s.length(); j++) {
                if (!(s.charAt(j)>='a'&&s.charAt(j)<='f')){
                    flag=false;
                }
            }
            if (flag){
                int parseInt = Integer.parseInt(s, 16);
                System.out.println(parseInt);
                break;
            }
            i++;
        }
    }
}
2730

第二题

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

public class Main {
    public static void main(String[] args) {
        System.out.println((char) ('A'+(2022/(26*26)-1)));
        System.out.println((char) ('A'+(2022%(26*26))/26-1));
        System.out.println((char) ('A'+(2022%(26*26))%26-1));
    }
}
BYT

第三题

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

import java.time.LocalDate;

public class Main {
    public static void main(String[] args) {
        LocalDate start = LocalDate.of(1900, 1, 1);
        LocalDate end = LocalDate.of(9999, 12, 31);
        int ans=0;
        while (!start.toString().equals(end.toString())){
            if (check(start.toString())) ans++;
            start=start.plusDays(1);
        }
        System.out.println(ans);
    }

    static boolean check(String start){
        int sum1=0;
        int sum2=0;
        String[] split = start.split("-");
        for (int i = 0; i < split[0].length(); i++) {
            sum1+=Integer.parseInt(""+split[0].charAt(i));
        }
        for (int i = 0; i < split[1].length(); i++) {
            sum2+=Integer.parseInt(""+split[1].charAt(i));
        }
        for (int i = 0; i < split[2].length(); i++) {
            sum2+=Integer.parseInt(""+split[2].charAt(i));
        }
        return sum1==sum2;
    }
}

70910

第四题

小蓝有 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

第五题

小蓝有一个 30 行 60 列的数字矩阵,矩阵中的每个数都是 0 或 1 。
  
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

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
    static int N = 1010;
    static int[] dx = {1, -1, 0, 0};
    static int[] dy = {0, 0, 1, -1};
    static int[][] arr = new int[100][100];
    static boolean[][] visit = new boolean[100][100];

    //表示当前岛屿是否存在不被淹没的陆地
    static boolean flag = false;
    static int n;

    static int ans;

    static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args) throws IOException {
        for (int i = 0; i <30; i++) {
            String[] s = br.readLine().split("");
            for (int j = 0; j <60; j++) {
                arr[i][j] = Integer.parseInt(s[j]);
            }
        }
        int max=0;
        for (int i = 0; i <30; i++) {
            for (int j = 0; j <60; j++) {
                if (arr[i][j] == 1 && !visit[i][j]) {
                    ans=1;
                    Queue<int[]> q = new LinkedList<>();
                    visit[i][j] = true;
                    q.offer(new int[]{i, j});
                    bfs(arr, q);
                    if (max<ans) max=ans;
                }
            }
        }
        System.out.println(max);
    }

    static void bfs(int[][] arr, Queue<int[]> q) {
        while (!q.isEmpty()) {
            int size = q.size();
            while (size-- > 0) {
                int[] curr = q.poll();
                int x = curr[0];
                int y = curr[1];
                for (int i = 0; i < 4; i++) {
                    int nx = x + dx[i];
                    int ny = y + dy[i];
                    if (nx >= 0 && nx <30 && ny >= 0 && ny < 60 && arr[nx][ny] == 1 && !visit[nx][ny]) {
                        ans++;
                        q.offer(new int[]{nx, ny});
                        visit[nx][ny] = true;
                    }
                }
            }
        }
    }

    static boolean check(int[][] arr, int i, int j) {
        return arr[i][j] == 1;
    }
}


148

第六题

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

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int w=sc.nextInt();
        int n=sc.nextInt();
        n%=7;
        while (n>0){
            if (w>=7){
                w=0;
            }
            w++;
            n--;
        }
        System.out.println(w);
    }
}

第七题

小蓝负责一块区域的信号塔安装,整块区域是一个长方形区域,建立坐标轴后,西南角坐标为 (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。

import java.io.*;

public class Main {
    static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] s = br.readLine().split(" ");
        int w=Integer.parseInt(s[0]);
        int h=Integer.parseInt(s[1]);
        int n=Integer.parseInt(s[2]);
        int r=Integer.parseInt(s[3]);
        boolean[][] visit=new boolean[w+1][h+1];
        int ans=0;
        while (n-->0){
            s = br.readLine().split(" ");
            int x=Integer.parseInt(s[0]);
            int y=Integer.parseInt(s[1]);
            for (int i=0;i<=w;i++){
                for (int j=0;j<=h;j++){
                    if (Math.sqrt((i-x)*(i-x)+(j-y)*(j-y))<=r&&!visit[i][j]) {
                        visit[i][j]=true;
                        ans++;
                    }
                }
            }
        }
        out.println(ans);
        out.flush();
    }
}

第八题

小蓝有一个 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。

import java.io.*;

public class Main {
    static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
    public static void main(String[] args) throws IOException {
        String[] s = br.readLine().split(" ");
        int n=Integer.parseInt(s[0]);
        int m=Integer.parseInt(s[1]);
        int t=Integer.parseInt(br.readLine());
        boolean[] visit=new boolean[n*m];
        while (t-->0){
            s = br.readLine().split(" ");
            int r1=Integer.parseInt(s[0]);
            int c1=Integer.parseInt(s[1]);
            int r2=Integer.parseInt(s[2]);
            int c2=Integer.parseInt(s[3]);
            for (int i=r1;i<=r2;i++){
                for (int j=c1;j<=c2;j++){
                    visit[(i-1)*m+j-1]=true;
                }
            }
        }
        int ans=0;
        for (int i=0;i<n*m;i++){
            if (!visit[i]) ans++;
        }
        out.println(ans);
        out.flush();
    }
}

第九题

小蓝准备在一个空旷的场地里面滑行,这个场地的高度不一,小蓝用一个 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。

import java.util.Scanner;

public class Main9{
	
	static int n, m;
	static int[][] g = new int[110][110];
	static int[][] f = new int[110][110];
	static int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
	
	static int dfs(int x, int y) {
		if (f[x][y] != -1)
	        return f[x][y];
	    
	    int res = 0;
	    for (int i = 0; i < 4; ++ i )
	    {
	        int tx = x + dx[i], ty = y + dy[i];
	        if (tx <= 0 || ty <= 0 || tx > n || ty > m || g[tx][ty] >= g[x][y])
	            continue;
	        res = Math.max(res, dfs(tx, ty));
	    }
	    
	    f[x][y] = res + 1;
	    return f[x][y];
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		m = sc.nextInt();
		
		for (int i = 1; i <= n; ++ i )
	        for (int j = 1; j <= m; ++ j )
	        	g[i][j] = sc.nextInt();
		
		for (int i = 1; i <= n; ++ i )
	        for (int j = 1; j <= m; ++ j )
	        	f[i][j] = -1; 
		int res = 0;
		for (int i = 1; i <= n; ++ i )
	        for (int j = 1; j <= m; ++ j )
	        	res = Math.max(res, dfs(i, j));
		System.out.println(res);
	}
	
}

第十题

小蓝有一个序列 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。

import java.io.*;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        int n = Integer.parseInt(br.readLine());
        int[] a = new int[n + 1];
        int[] b = new int[n + 1];
        //l统计最小值 r是最大值
        List<Integer> l = new ArrayList<>();
        List<Integer> r = new ArrayList<>();
        String[] s = br.readLine().split(" ");
        for (int i = 1; i <= n; i++) {
            a[i] = Integer.parseInt(s[i - 1]);
        }
        for (int i=n;i>=1;i--){
            b[n-i+1]=a[i];
        }
        int k = Integer.parseInt(br.readLine());
        Deque<Integer> q = new ArrayDeque();
        for (int i = 1; i <= n; i++) {
            if (!q.isEmpty() && q.peekFirst() < i - k) q.pollFirst();
            while (!q.isEmpty() && a[q.peekLast()] > a[i]) q.pollLast();
            q.addLast(i);
            l.add(q.peekFirst());
        }
        q.clear();
        for (int i = 1; i <=n ; i++) {
            if (!q.isEmpty() && q.peekFirst() < i - k) q.pollFirst();
            while (!q.isEmpty() && b[q.peekLast()] > b[i]) q.pollLast();
            q.addLast(i);
            r.add(q.peekFirst());
        }
        for (int i = 0; i < r.size(); i++) {
            out.print(Math.min(a[l.get(i)], b[r.get(n - i - 1)]) + " ");
        }
        out.flush();
    }
}
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个双子座的Java攻城狮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值