蓝桥杯日期问题

蓝桥杯其他真题点这里👈

注意日期合法的判断

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;
    }
}
相关推荐
普通网友3 分钟前
C++编译期数据结构
开发语言·c++·算法
Gorgous—l4 分钟前
数据结构算法学习:LeetCode热题100-图论篇(岛屿数量、腐烂的橘子、课程表、实现 Trie (前缀树))
数据结构·学习·算法
代码程序猿RIP10 分钟前
【C++开发面经】全过程面试问题详解
java·c++·面试
whatever who cares11 分钟前
Java/Android中BigDecimal的相关操作
android·java·开发语言
im_AMBER12 分钟前
算法笔记 13 BFS | 图
笔记·学习·算法·广度优先
普通网友32 分钟前
嵌入式C++安全编码
开发语言·c++·算法
烤麻辣烫37 分钟前
黑马程序员苍穹外卖(新手) DAY3
java·开发语言·spring boot·学习·intellij-idea
妮妮喔妮42 分钟前
JAVA反射的介绍(优缺点)
java·开发语言
i***48611 小时前
Redis重大版本整理(Redis2.6-Redis7.0)
java·数据库·redis
驯狼小羊羔1 小时前
学习随笔-hooks和mixins
前端·javascript·vue.js·学习·hooks·mixins