蓝桥杯刷题3

目录:

[1. 天干地支](#1. 天干地支)

[2. 明明的随机数](#2. 明明的随机数)

[3. ISBN号码](#3. ISBN号码)

[4. 缩位求和](#4. 缩位求和)

[5. 幸运数字](#5. 幸运数字)

[6. 串的处理](#6. 串的处理)

[7. 最长递增](#7. 最长递增)

[8. 灌溉](#8. 灌溉)

[9. 特殊日期](#9. 特殊日期)

[10. 最大距离](#10. 最大距离)


1. 天干地支

java 复制代码
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int year = scan.nextInt();
        String tiangan[] = {"geng","xin","ren","gui","jia","yi","bing","ding","wu","ji"};
        String dizhi[] = {"shen","you","xu","hai","zi","chou","yin","mao","chen","si","wu","wei"};
        System.out.println(tiangan[year%10] + dizhi[year%12]);
        scan.close();
        //40 40%12 = 4
        //0:gengshen
    }
}

求出公元0年的天干地支年,重新排序

2. 明明的随机数

java 复制代码
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int num = scan.nextInt();
        HashSet<Integer> studentSet = new HashSet<>();
        
        for(int i = 0; i < num; i++){
            int score = scan.nextInt();
            studentSet.add(score);
        }
        //将一个包含整数的HashSet(studentSet)转换为一个整数数组(studentArray)
        Integer[] studentArray = studentSet.toArray(new Integer[0]);
        Arrays.sort(studentArray);

        System.out.println(studentArray.length);
        for(int i = 0; i < studentArray.length; i++){
            System.out.print(studentArray[i] + " ");
        }
    }
}

3. ISBN号码

java 复制代码
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String s = scan.next();
        String str = s.replace("-","");
        int sum = 0;
        for(int i =0;i<9;i++){
          sum += Integer.parseInt(str.charAt(i)+"") * (i+1);
        }
        
        if((sum%11<10 && sum%11==Integer.parseInt(str.charAt(9)+"")) || (sum%11==10&&str.charAt(9)=='X')){
          System.out.println("Right");
        }else{
          if(sum%11<10){
          System.out.println(s.substring(0,12)+sum%11);
          }else if(sum%11==10){
            System.out.println(s.substring(0,12)+"X");
          }
        }
        scan.close();
    }
}

在Java中,charAt()方法返回的是一个字符类型。当你需要将字符转换为可以被Integer.parseInt()处理的字符串时,需要将其转换为字符串类型。虽然字符本身可以隐式地转换为字符串,但在实际编程中,直接写成 str.charAt(i) + "" 的形式是为了增加代码的可读性,明确表示你想要将字符转换为字符串。

这里的 (str.charAt(i) + "") 相当于显式地将字符转换为了长度为1的字符串 。在本例中,由于 Integer.parseInt() 需要接收一个代表整数的字符串作为参数,因此这样的转换是必要的。

4. 缩位求和

java 复制代码
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        BigInteger num = scan.nextBigInteger();
        while(num.compareTo(BigInteger.valueOf(9)) > 0){
          num = num.mod(BigInteger.TEN).add(num.divide(BigInteger.TEN));
        }
        System.out.println(num);
        scan.close();
    }
}

5. 幸运数字

java 复制代码
import java.util.*;

public class Main {
    public static void main(String[] args) {
        int count = 0;
        for(int i =1;;i++){
          if(check(i,2)==0 && check(i,8)==0 && check(i,10)==0 && check(i,16)==0){
            count++;
          }
          if(count == 2023){
            System.out.println(i);
            break;
          }
        }
    }

    public static int check(int i,int jinzhi){
      int temp = i;
      int he = 0;
      while(i > 0){
        he += i % jinzhi;
        i = i / jinzhi;
      }
      return temp % he;
    }
}

6. 串的处理

java 复制代码
import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        //将一个字符串按照指定的分隔符拆分成字符串数组
        //表示匹配一个或多个空白字符(包括空格、制表符、换行符等)
        String str[] = s.split("\\s+");
        for (int i = 0; i < str.length; i++) {
          //将当前字符串 str[i] 的第一个字符转换为大写并将剩余部分连接起来
          str[i] = str[i].substring(0, 1).toUpperCase() + str[i].substring(1);
          str[i] = str[i].replaceAll("(\\d)([a-zA-Z])", "$1_$2");
          str[i] = str[i].replaceAll("([a-zA-Z])(\\d)", "$1_$2");
          System.out.print(str[i] + " ");
        }
    }
}
  • (\\d):匹配一个数字,并将其捕获到组1中。
  • ([a-zA-Z]):匹配一个字母,并将其捕获到组2中。

替换字符串是 "$1_$2" ,其中 $1$2 是反向引用,分别代表匹配到的第一个和第二个捕获组的内容。所以这个操作会查找数字紧接着字母的情况,并在它们之间插入一个下划线。例如,如果 str[i] = "3apple",经过这个操作后,结果变为 "3_apple"

7. 最长递增

java 复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int num = scan.nextInt();
        int arr[] = new int[num];
        for(int i = 0;i<arr.length;i++){
          arr[i] = scan.nextInt();
        }
        
        int result = 0;
        int count = 1;
        for (int i = 1; i < num; i++) {
            if(arr[i] > arr[i-1]){
              count++;
              result = result>count ? result:count;
            }else{
              count = 1;
            }
        }
        System.out.println(result);
        scan.close();
    }
}

8. 灌溉

java 复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
        int count1 = scan.nextInt();
        int arr[][] = new int[n][m];
        for(int i = 0;i<count1;i++){
          arr[scan.nextInt()-1][scan.nextInt()-1] = 1;
        }
        int k = scan.nextInt();

        int count = 1;
        for(int i = 0;i<k;i++){
          count ++;
          for(int hang = 0;hang<n;hang++){
            for (int lie = 0;lie<m;lie++){
              if(arr[hang][lie] == count-1){
                if(hang -1 >= 0){
                  arr[hang-1][lie] = count;
                }
                if(hang +1 < n){
                  arr[hang+1][lie] = count;
                }
                if(lie - 1 >= 0){
                  arr[hang][lie-1] = count;
                }
                if(lie+1 < m){
                  arr[hang][lie+1] = count;
                }
              }
            }
          }
        }
        
        int num = 0;
        for(int i =0;i<n;i++){
          for(int j = 0;j<m;j++){
            if(arr[i][j] > 0){
              num ++;
            }
          }
        }
        System.out.println(num);
        scan.close();
    }
}

9. 特殊日期

java 复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int year = 1900;
        int day = 1;
        int month = 1;
        int months[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
        
        int count = 0;
        while(true){
          if((year%4 == 0 && year%100 != 0) || year%400 == 0){
          months[2] = 29;
          }else{
            months[2] = 28;
          }
          if(year/1000 + year/100%10 + year/10%10 + year%10 == month/10 + month%10 + day/10 + day%10){
            count++;
          }
          day++;
          if(day>months[month]){
            day = 1;
            month++;
            if(month>12){
              month = 1;
              year++;
            }
          }
          
          if(year==9999 && month==12 && day==31){
            break;
          }
        }
        System.out.println(count);
    }

  
}

10. 最大距离

java 复制代码
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int num = scan.nextInt();
        int arr[] = new int[num];
        for(int i = 0;i<arr.length;i++){
          arr[i] = scan.nextInt();
        }
        int max = 0;
        for(int i = 0;i<arr.length;i++){
          for(int j = i+1;j<arr.length;j++){
            int b = Math.abs(i - j) + Math.abs(arr[i] - arr[j]);
            if(b > max){
              max = b;
            }
          }
        }
        System.out.println(max);
        scan.close();
    }
}
相关推荐
折哥的程序人生 · 物流技术专研4 小时前
Java面试85题图解版 · 特别篇:2026后端高频面试题复盘(算法底层逻辑+高并发架构设计全解析,附Java实战代码)
java·网络·数据库·算法·面试
AOwhisky4 小时前
Redis 学习笔记(第三期):持久化与主从复制
运维·数据库·redis·笔记·学习·云计算
想吃火锅10055 小时前
【leetcode】14.最长公共前缀js
算法·leetcode·职场和发展
Tbisnic5 小时前
AI大模型学习第十一天:技术选型、安全防护与金融实战
python·学习·ai·大模型·提示词工程
云絮.7 小时前
数据库操作
数据库·mysql·算法·oracle
小林ixn7 小时前
LeetCode 206. 反转链表(迭代 + 递归详解)
算法·leetcode·链表
xmtxz7 小时前
计算机网络基础课程学习心得:从理论抽象到硬核实战的进阶之路
运维·学习
凡人叶枫7 小时前
Effective C++ 条款17:以独立语句将 newed 对象置入智能指针
java·linux·开发语言·c++·算法
YM52e8 小时前
男孩子在外自我保护指南——用鸿蒙 ArkTS 构建交互式安全教育应用
学习·安全·华为·harmonyos·鸿蒙·鸿蒙系统
菜鸟‍8 小时前
LeetCode 1 27 和 704 || 两数之和 移除元素 二分查找
算法·leetcode·职场和发展