蓝桥杯刷题7

目录

1. 字母数

2. 列名

3. 大乘积

4. 最大连通

5. 星期几


1. 字母数

public class Main {
    public static void main(String[] args) {
        int num = 2023;
        while(true) {
            String m=Integer.toString(num,16);
            if(m.matches("^[a-f]+$")){
                System.out.println(num);
                break;
            }
            num++;
        }
    }
}
  • ^ 表示匹配字符串的开始位置。
  • [a-f] 是一个字符集,表示匹配任何一个在其中的字符。这里的 a-f 包含了小写字母a到f。
  • + 代表前面的字符集出现一次或多次,也就是说整个字符串必须由一个或多个连续的a到f之间的字母组成。
  • $ 表示匹配字符串的结束位置。

2. 列名

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

3. 大乘积

public class Main {
    public static void main(String[] args) {
        int count = 0;
        int[] nums = {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<nums.length; ++i){
          for (int j=i+1; j<nums.length; ++j){{
            if (i != j){
              if (nums[i]*nums[j]>=2022){
                count++;
              }
            }
          }}
        }

        System.out.println(count);
    }
}

4. 最大连通

连通性判断是DFS最常见的应用。连通性判断是图论中的一个简单问题,给定一张图,图由点和边组成,要求找到互相连通的部分。连通性判断有三种实现方法:BFS、DFS、并查集,用DFS最简单方便。
  在竞赛题中,图常常用方格图给出,每个方格可以向上下左右四个方向走。
  DFS判断连通性,步骤如下:
  (1)从任意一个点u开始遍历,标记u已经搜过。一般从第一个点开始。
  (2)DFS搜索u的所有符合连通条件的邻居点。已经搜过的点标记为已经搜过,后面不用再搜。扩展u的邻居点时,应该判断这个邻居点是不是在边界内。
  (3)DFS结束,找到了与u连通的所有点,这是一个连通块。
  (4)不与u连通的、其他没有访问到的点,继续用上述步骤处理,找到所有的连通块。

import java.util.Scanner;
public class Main {
    static int[] dx = {-1, 0, 1, 0};
    static int[] dy = {0, 1, 0, -1};//四个方向
    static char[][] g;
    static int n = 30, m = 60;

    static int dfs(int x, int y) {//当前位于坐标[x,y]
        if (g[x][y] == '0') return 0;
        g[x][y] = '0';//把这个点从1改为0,后面不再搜它
        int cnt = 1;//统计这个连通块的大小
        for (int i = 0; i < 4; i++) { //遍历它的4个邻接
            int nx = x + dx[i], ny = y + dy[i];//一个邻居的坐标
            if (nx < 0 || ny < 0 || nx >= n || ny >= m) continue;//这个邻居是否在边界内
            cnt += dfs(nx, ny);
        }
        return cnt;
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        g = new char[n][m];

        for (int i = 0; i < n; i++){
//g[i] = scanner.nextLine().toCharArray(); 这行代码的用法是将用户从控制台输入的一行文本转化为字符数组,并将其赋值给二维字符数组 g 的第 i 行。
          g[i] = scanner.nextLine().toCharArray();
        }        
        int ans = 0;
        for (int i = 0; i < n; i++) 
            for (int j = 0; j < m; j++) 
                if (g[i][j] == '1') 
                    ans = Math.max(ans, dfs(i, j));
        System.out.println(ans);
    }
}

5. 星期几

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int w = scan.nextInt();
        int n = scan.nextInt();
        int m = (n+w)%7;
        if(m==0){
          System.out.println(7);
        }else{
          System.out.println(m);
        }
        scan.close();
    }
}

相关推荐

  1. -每日-023

    2024-03-13 22:24:03       30 阅读
  2. -每日-024

    2024-03-13 22:24:03       30 阅读
  3. -每日-026

    2024-03-13 22:24:03       42 阅读
  4. -每日-027

    2024-03-13 22:24:03       34 阅读
  5. -每日-029

    2024-03-13 22:24:03       36 阅读
  6. --python-2

    2024-03-13 22:24:03       28 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-13 22:24:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-13 22:24:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-13 22:24:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-13 22:24:03       20 阅读

热门阅读

  1. 学习记录之数学表达式(1)

    2024-03-13 22:24:03       17 阅读
  2. ReentrantReadWriteLock学习

    2024-03-13 22:24:03       19 阅读
  3. Python中,括号内部的for循环(列表推导式)

    2024-03-13 22:24:03       20 阅读
  4. 使用Ora2Pg迁移oracle数据到openGauss

    2024-03-13 22:24:03       18 阅读
  5. Qualcomm AI Hub-API介绍

    2024-03-13 22:24:03       23 阅读
  6. C++标准输入输出和名字空间

    2024-03-13 22:24:03       27 阅读
  7. C#接口和泛型

    2024-03-13 22:24:03       21 阅读