蓝桥杯日期问题

蓝桥杯其他真题点这里👈

注意日期合法的判断

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;
    }
}
相关推荐
python零基础入门小白3 分钟前
LangGraph智能体实战:如何用Langfuse构建AI运行时全链路可观测系统?
人工智能·学习·ai·chatgpt·程序员·大模型·智能体
极小狐8 分钟前
从复制粘贴到可复用组件:极狐GitLab CI/CD 组件目录实战
java·ci/cd·gitlab·devops·组件化
绿算技术11 分钟前
Solidigm联合绿算技术共同发布《面向 SOHO AI 推理的存储扩展方案》技术白皮书
人工智能·科技·算法·架构·spark
麻瓜code13 分钟前
【Agent】Spring AI RAG 实战:本地向量库 + 云端知识库
java·人工智能·spring
小刘在重生~33 分钟前
Java 集合|Collection、List、ArrayList、LinkedList、泛型、Collections 工具类
java·数据结构·list
EDPJ42 分钟前
(2026|IPI|我的论文投稿中,PSP 超轻量采样算法,轻量化架构探索/早融合+头压缩)LUMIN:面向工业异常检测的轻量级通用制造检测网络
算法·计算机视觉·架构·异常检测·采样算法
我不会起名字3221 小时前
一天一道算法题(29):单调栈
java·数据结构·python·算法·leetcode·golang·单调栈
XR1234567881 小时前
医院移动医护零漫游无线网络选型指南
java·后端·struts
z23456d1 小时前
第三十九天学习心得
学习
干到60岁退休的码农1 小时前
13.构建登录接口响应数据
java·spring boot·mybatis