蓝桥杯日期问题

蓝桥杯其他真题点这里👈

注意日期合法的判断

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;
    }
}
相关推荐
gadiaola2 分钟前
【JVM】Java虚拟机(二)——垃圾回收
java·jvm
眼镜哥(with glasses)3 分钟前
蓝桥杯 国赛2024python(b组)题目(1-3)
数据结构·算法·蓝桥杯
coderSong25683 小时前
Java高级 |【实验八】springboot 使用Websocket
java·spring boot·后端·websocket
Mr_Air_Boy4 小时前
SpringBoot使用dynamic配置多数据源时使用@Transactional事务在非primary的数据源上遇到的问题
java·spring boot·后端
豆沙沙包?4 小时前
2025年- H77-Lc185--45.跳跃游戏II(贪心)--Java版
java·开发语言·游戏
年老体衰按不动键盘4 小时前
快速部署和启动Vue3项目
java·javascript·vue
恰薯条的屑海鸥4 小时前
零基础在实践中学习网络安全-皮卡丘靶场(第十六期-SSRF模块)
数据库·学习·安全·web安全·渗透测试·网络安全学习
咖啡啡不加糖4 小时前
Redis大key产生、排查与优化实践
java·数据库·redis·后端·缓存
liuyang-neu5 小时前
java内存模型JMM
java·开发语言
int型码农5 小时前
数据结构第八章(一) 插入排序
c语言·数据结构·算法·排序算法·希尔排序