蓝桥杯日期问题

蓝桥杯其他真题点这里👈

注意日期合法的判断

java 复制代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main{
    static int[] days = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    static BufferedReader in = new BufferedReader(
            new InputStreamReader(System.in));

    public static void main(String[] args) throws IOException {
        String[] init = in.readLine().split("/");
        int a = Integer.parseInt(init[0]);
        int b = Integer.parseInt(init[1]);
        int c = Integer.parseInt(init[2]);

        for (int i = 19600101; i < 20591231; i++) {
            int year = i / 10000;
            int month = i / 100 % 100;
            int day = i % 100;
            if (check(year,month,day)){
                if (year % 100 == a && month == b && day == c //abc
                || year % 100 == c && month == a && day == b  //cab
                || year % 100== c && month == b && day == a  //cba
                ) System.out.printf("%d-%02d-%02d\n",year,month,day);
            }
        }
        in.close();
    }
    public static boolean check(int year,int month,int day){
        //月份异常
        if (month == 0 || month > 12) return false;
        
        //天数异常
        if (day == 0 || month != 2 && day > days[month]) return false;
            
        //不是2月的情况可能会在这出现,比如1月32号
        if(month == 2){
            //单独判断月份为2时是否是闰年
        int res = 0;
        if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) res++;
        if (day > 28 + res) return false;
        
        }
        return true;
    }
}
相关推荐
学编程的闹钟3 小时前
PHP变量类型转换机制全解析
学习
t***44235 小时前
【Springboot3+vue3】从零到一搭建Springboot3+vue3前后端分离项目之后端环境搭建
java
daidaidaiyu6 小时前
SpringCloud 微服务实现一则 (Eureka + Hystrix)
java·spring
plus4s6 小时前
2月12日(70-72题)
算法
小哥不太逍遥6 小时前
Technical Report 2024
java·服务器·前端
m0_672703316 小时前
上机练习第24天
算法
逆境清醒6 小时前
软考高项常见英语词汇(2)
职场和发展·信息系统项目管理师·英语·计算机软考
edisao6 小时前
序幕-内部审计备忘录
java·jvm·算法
shehuiyuelaiyuehao7 小时前
22Java对象的比较
java·python·算法
AI360labs_atyun7 小时前
字节AI双王炸来了!Seedance 2.0 + Seedream 5.0
人工智能·科技·学习·百度·ai