【第二天】蓝桥杯备战-顺子日期

时间:2024-01-27 18:09:07

https://www.lanqiao.cn/problems/2096/learning/
在这里插入图片描述

解法一:

public class Main{
    public static void main(String[] args){

        //2022起始日期
        int n = 20220101;

        //顺子数组
        String [] str = {"012","123","234","345","456","567","678","789"};

        int count = 0;
        while (n <= 20221231){
            //如果日期合法
            if (judge(n)){
                //如果存在顺子
                if (func(n+"",str)){
                    count++;
                }
            }
            n++;
        }
        System.out.println(count);
    }

    //判断日期是否合法
    public static boolean judge(int n){
        //2022年是平年,2月只有28天
        int[] arr = {31,28,31,30,31,30,31,31,30,31,30,31};

        int month = n % 10000 / 100;
        int day = n % 100;

        if(day <= arr[month-1] && day > 0) {
            return true;
        }
        return false;

    }

    //判断顺子
    public static boolean func(String s,String [] str){
        for( int i = 0 ; i < str.length ; i++ ){
            if (s.contains(str[i])){
                return true;
            }
        }
        return false;
    }
    
}

解法二:暴力穷举

import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    public static void main(String[] args) {
        System.out.println(14);
20220120
20220121
20220122
20220123
20220124
20220125
20220126
20220127
20220128
20220129
20221012
20221123
20221230
20221231
    }
}