蓝桥杯日期问题

蓝桥杯其他真题点这里👈

注意日期合法的判断

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;
    }
}
相关推荐
settingsun12253 分钟前
【AI-算法-01】ResNet (残差网络) & Skip Connections
人工智能·算法
muxin-始终如一10 分钟前
Maven HTTP 仓库被阻止问题解决总结
java·http·maven
武斌14 分钟前
需要独立的作业队列?看看Quartz增强框架Quartz Plus
java·spring boot·后端
橘颂TA22 分钟前
【剑斩OFFER】算法的暴力美学——两数之和
数据结构·算法·leetcode·力扣·结构与算法
福楠31 分钟前
C++ STL | vector
开发语言·c++·算法
廋到被风吹走32 分钟前
【Java】【JVM】即时编译解析:C1/C2、分层编译、OSR与日志分析
java·开发语言·jvm
计算机毕设指导634 分钟前
基于微信小程序的扶贫助农系统【源码文末联系】
java·spring boot·后端·微信小程序·小程序·tomcat·maven
云里雾里!35 分钟前
力扣 268. 缺失数字 ✅ 【位运算(异或)最优解法】深度解析
算法·leetcode
Overt0p39 分钟前
抽奖系统(5)
java·redis·springboot
YJlio39 分钟前
PsPing 学习笔记(14.7):一条龙网络体检脚本——连通性、延迟、带宽全都要
开发语言·网络·笔记·python·学习·pdf·php