Acwing1229.日期问题java题解

 算法标签:枚举 模拟 日期问题

把年月日 月日年 日月年 以及19xx年或者20xx年一个6种可能的结果列出  判断①是否在19600101到20591231内②日期是否合法

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.util.*;

public class acwing1229日期问题 {

    public static void main(String[] args) throws Exception {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String str = br.readLine();

        String[] parts = str.split("/");

        String s1 = parts[0];

        int a = Integer.parseInt(s1) / 10 % 10;

        int b = Integer.parseInt(s1) % 10;

        String s2 = parts[1];

        int c = Integer.parseInt(s2) / 10 % 10;

        int d = Integer.parseInt(s2) % 10;

        String s3 = parts[2];

        int e = Integer.parseInt(s3) / 10 % 10;

        int f = Integer.parseInt(s3) % 10;

        ArrayList<String> validDates = new ArrayList<>();

 

        if (panduan(a, b, c, d, e, f, 2, 0)) {

            validDates.add(String.format("%d%d%d%d-%d%d-%d%d", 2, 0, a, b, c, d, e, f));

 

        }

        if (panduan(a, b, c, d, e, f, 1, 9)) {

            validDates.add(String.format("%d%d%d%d-%d%d-%d%d", 1, 9, a, b, c, d, e, f));

 

        }

 

        if (panduan(e, f, c, d, a, b, 1, 9)) {

            validDates.add(String.format("%d%d%d%d-%d%d-%d%d", 1, 9,e, f, c, d, a, b));

 

        }

        if (panduan(e, f, c, d, a, b, 2, 0)) {

            validDates.add(String.format("%d%d%d%d-%d%d-%d%d", 2, 0,e, f, c, d, a, b));

 

        }

 

 

        if (panduan(e, f, a, b, c, d, 1, 9)) {

            validDates.add(String.format("%d%d%d%d-%d%d-%d%d", 1, 9,e, f, a, b, c, d));

 

        }

        if (panduan(e, f, a, b, c, d, 2, 0)) {

            validDates.add(String.format("%d%d%d%d-%d%d-%d%d", 2, 0,e, f, a, b, c, d));

 

        }

        //Collections.sort(validDates);

        Set<String> set = new HashSet<>(validDates);

 

        // 将去重后的集合转换回列表

        List<String> uniqueList = new ArrayList<>(set);

 

        // 打印排序且去重后的日期

        //注意先去重后排序 因为如果先排序后再放入set去重 set是不保证顺序的 这时候顺序可能又被打乱

        Collections.sort(uniqueList);

        for (String date : uniqueList) {

            System.out.println(date);

        }

    }

    public static boolean panduan(int a,int b,int c,int d,int e,int f,int t1,int t2) {

        boolean t=false;

        int day = e * 10 + f;

        int month = c * 10 + d;

        int year = t1*1000+t2*100 + a * 10 + b;//1900和2000

        int sum=t1*10000000+t2*1000000+a*100000+b*10000+e*1000+f*100+c*10+d;

        if(sum>=19600101&&sum<=20591231){

            if (month >= 1 && month <= 12) {

                if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {

                    if (day >= 1 && day <= 31) {

                        t=true;

                    }

                }

                if (month == 4 || month == 6 || month == 9 || month == 11) {

                    if (day >= 1 && day <= 30) {

                        t=true;

                    }

                }

                if (month == 2) {

                    if (year % 400 == 0 && day >= 1 && day <= 29) {

                        t=true;

                    }

                    if (year % 4 != 0 && day >= 1 && day <= 28) {

                        t=true;

                    }

                    if (year % 4 == 0 && year % 100 != 0 && day >= 1 && day <= 29) {

                        t=true;

                    }

                }

            }

        }

        return t;

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值