蓝桥杯日期问题

蓝桥杯其他真题点这里👈

注意日期合法的判断

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;
    }
}
相关推荐
indexsunny7 分钟前
互联网大厂Java面试实战:从Spring Boot到微服务架构的技术问答解析
java·spring boot·redis·微服务·kafka·jwt·flyway
蓁蓁啊10 分钟前
C/C++编译链接全解析——gcc/g++与ld链接器使用误区
java·c语言·开发语言·c++·物联网
XX風11 分钟前
3.2K-means
人工智能·算法·kmeans
sheji341613 分钟前
【开题答辩全过程】以 基于SpringBoot的疗养院管理系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
tb_first26 分钟前
万字超详细苍穹外卖学习笔记2
java·jvm·数据库·spring·tomcat·maven
短剑重铸之日37 分钟前
《设计模式》第六篇:装饰器模式
java·后端·设计模式·装饰器模式
像少年啦飞驰点、39 分钟前
零基础入门 Spring Boot:从‘Hello World’到可上线微服务的完整学习路径
java·spring boot·web开发·编程入门·后端开发
心 -41 分钟前
全栈实时聊天室(java项目)
java
czxyvX1 小时前
016-二叉搜索树(C++实现)
开发语言·数据结构·c++