2月29日

通过好多遍终于AC了,我承认我很挫。。。
时间限制:2000ms
单点时限:1000ms
内存限制:256MB
描述
给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期)。

只有闰年有2月29日,满足以下一个条件的年份为闰年:

  1. 年份能被4整除但不能被100整除

  2. 年份能被400整除

输入
第一行为一个整数T,表示数据组数。

之后每组数据包含两行。每一行格式为”month day, year”,表示一个日期。month为{“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November” , “December”}中的一个字符串。day与year为两个数字。

数据保证给定的日期合法且第一个日期早于或等于第二个日期。

输出
对于每组数据输出一行,形如”Case #X: Y”。X为数据组数,从1开始,Y为答案。

数据范围
1 ≤ T ≤ 550

小数据:

2000 ≤ year ≤ 3000

大数据:

2000 ≤ year ≤ 2×109

样例输入
4
January 12, 2012
March 19, 2012
August 12, 2899
August 12, 2901
August 12, 2000
August 12, 2005
February 29, 2004
February 29, 2012
样例输出
Case #1: 1
Case #2: 0
Case #3: 1
Case #4: 3

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int q=1;
        while (n != 0) {
            int count = 0;
            String month1 = in.next();
            String day1 = in.next();
            String a[]=day1.split(",");
            int day_1=Integer.parseInt(a[0]);
            int year1 = in.nextInt();
            String month2 = in.next();
            String day2 = in.next();
            String b[]=day2.split(",");
            int day_2=Integer.parseInt(b[0]);
            int year2 = in.nextInt();
            for (int i = year2; i > year1; i--) {
                if (i % 400 == 0 || (i % 4 == 0 && (i % 100 != 0))) {
                    if (i == year2) {
                        if (!(month2.equals("January") || ((month2
                                .equals("February") && day_2 != 29)))) {
                            count++;
                        }
                    } else {
                        count++;
                    }
                }
            }

            if (year1 % 400 == 0 || (year1 % 4 == 0 && (year1 % 100 != 0))) {
                if (year2 == year1) {
                    if (((month1.equals("January")) || (month1
                            .equals("February") && day_1 == 29))
                            && (!(month2.equals("January") || (month2
                                    .equals("February") && day_2 < 29))))
                        count = count + 1;
                } else {
                    if (((month1.equals("January")) || (month1
                            .equals("February") && day_1 <= 29))) {
                        count = count + 1;
                    }
                }
            }
            System.out.println("Case #"+q+": "+count);
            q++;
            n--;
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值