蓝桥杯刷题7

目录

[1. 字母数](#1. 字母数)

[2. 列名](#2. 列名)

[3. 大乘积](#3. 大乘积)

[4. 最大连通](#4. 最大连通)

[5. 星期几](#5. 星期几)


1. 字母数

java 复制代码
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. 列名

java 复制代码
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. 大乘积

java 复制代码
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连通的、其他没有访问到的点,继续用上述步骤处理,找到所有的连通块。

java 复制代码
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. 星期几

java 复制代码
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();
    }
}
相关推荐
熊猫_豆豆5 分钟前
YOLOP车道检测
人工智能·python·算法
rannn_1116 分钟前
【苍穹外卖|Day4】套餐页面开发(新增套餐、分页查询、删除套餐、修改套餐、起售停售)
java·spring boot·后端·学习
qq_12498707539 分钟前
基于JavaWeb的大学生房屋租赁系统(源码+论文+部署+安装)
java·数据库·人工智能·spring boot·计算机视觉·毕业设计·计算机毕业设计
短剑重铸之日16 分钟前
《设计模式》第十一篇:总结
java·后端·设计模式·总结
艾莉丝努力练剑20 分钟前
【Linux:文件】Ext系列文件系统(初阶)
大数据·linux·运维·服务器·c++·人工智能·算法
张人玉20 分钟前
VisionPro 定位与卡尺测量学习笔记
笔记·学习·计算机视觉·vsionprp
若鱼191938 分钟前
SpringBoot4.0新特性-Observability让生产环境更易于观测
java·spring
觉醒大王1 小时前
强女思维:着急,是贪欲外显的相。
java·论文阅读·笔记·深度学习·学习·自然语言处理·学习方法
偷吃的耗子1 小时前
【CNN算法理解】:CNN平移不变性详解:数学原理与实例
人工智能·算法·cnn