蓝桥杯日期问题

蓝桥杯其他真题点这里👈

注意日期合法的判断

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;
    }
}
相关推荐
bIo7lyA8v9 分钟前
算法优化中的多线程数据一致性问题的技术8
算法
東隅已逝,桑榆非晚10 分钟前
数据结构:算法效率与复杂度分析详解
数据结构·笔记·算法
闪电悠米12 分钟前
黑马点评-Redisson-02_reentrant_lock
java·spring boot·redis·分布式·缓存
凌波粒14 分钟前
LeetCode--236. 二叉树的最近公共祖先(二叉树)
算法·leetcode·职场和发展
云烟成雨TD16 分钟前
Spring AI Alibaba 1.x 系列【67】ReactAgent SSE 流式输出
java·人工智能·spring
半夜修仙16 分钟前
分治思想对数组进行排序-归并排序
数据结构·算法·排序算法
数智工坊18 分钟前
周志华《Machine Learning》学习笔记--第六章--支持向量机
笔记·神经网络·学习·算法·机器学习·支持向量机
casual~20 分钟前
【学习记录】
学习·算法
社交怪人23 分钟前
【奇偶ASCII值】信息学奥赛一本通C语言解法(题号1042)
算法