回文日期

//每次枚举前四位,把前四位反转拼接到后面去,这样就是在回文数里判断一个数是不是合法日期


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main{
    static int n,m,res;
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            //这里要从0开始,才会跟月份对应
    static int[] days = {0,31,28,31,30,31,30,31,31,30,31,30,31};

    public static void main(String[] args)throws IOException {
        n = Integer.parseInt(in.readLine());
        m = Integer.parseInt(in.readLine());
        
        //只枚举前四位
        for (int i = 1000; i < 9999; i++) {
            int x = i,t = i;
            //将前四位反转拼接
            for (int j = 0; j < 4; j++) {
                x = x * 10 + t % 10;
                t /= 10;
            }
            //合法日期并且符合题意就算一个答案
            if (x >= n && x <= m && check(x)) res++;

        }
        System.out.println(res);
        in.close();
    }
    
    //判断日期合法
    public static boolean check(int date){
        int year = date / 10000;    //丢掉后面四位
        int month = date / 100 % 100;   //丢掉后面两位拿到后面两位
        int day = date % 100;   //拿到后面两位
        
        //先判断月,再判断日,判断日的时候先不判断2月,2月单独判断
        if (month == 0 || month > 12) return false;
        if (day == 0 || (month != 2 &&day > days[month]))return false;
        
        //月份为2单独判断
        if (month == 2){
            int etra = 0;
            if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) etra++;
            if (day > 28 + etra) return false;
        }
        return true;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值