蓝桥杯刷题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();
    }
}
  • 9
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值