时间限制: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 {

    static String[] month={
"January","February","March","April","May","June","July",
"August","September","October","November","December"
};
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int a = s.nextInt();
//要加循环读入数据,每读入一次调用一次find229
int sum[] = new int[a];
String[] date1 = new String[3];
String[] date2 = new String[3];
for(int i = 0;i<a;i++){
for(int j =0;j<3;j++){
date1[j] = s.next();
}
for(int j =0;j<3;j++){
date2[j] = s.next();
}
sum[i] = find229(date1,date2);
}
for(int i = 0;i<a;i++){
System.out.println("Case #"+(i+1)+": "+sum[i]);
}
s.close();
}
public static int find229(String[] date1,String[] date2){
int year1 = Integer.valueOf(date1[2]);
int year2 = Integer.valueOf(date2[2]);
int day2 = Integer.valueOf(date2[1].split(",")[0]);
int result = 0;
for(int i = ((year1-1)/4+1)*4;i<=(year2/4)*4;i = i+4){//保证每个年份能被4整除
if(i%400 == 0){
//从某一个能被400整除的数开始,每400个数里面有96个能被4整除但不能被100整除的数,有一个能被400整除的数,算上尾,不算头
//比如400~800,则是从401~800有一个能被400整除的数,有96个能被4整除不能被100整除的数
result++;
result += (year2/400-i/400)*97;
//小于ed.year的最大的能被400整除的数
i = (year2/400)*400;
}else if(i%100!=0){//不能被400整除时
result++;
}
}
//考虑起止年份
if(year1%400==0||(year1%4==0&&year1%100!=0)){
if(!(month[0].equals(date1[0])||month[1].equals(date1[0]))){
result--;
}
}
if(year2%400==0||(year2%4==0&&year2%100!=0)){
if(month[0].equals(date2[0])){
result--;
}else if(month[1].equals(date2[0])&&day2<29){
result--;
}
}
return result;
}
}

Hihocoder 2月29日的更多相关文章

  1. 【hihoCoder】1148:2月29日

    问题:http://hihocoder.com/problemset/problem/1148 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期). 思路: 1. 将问题转换成求两个日 ...

  2. hihoCoder 1148 2月29日

    时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期). 只有闰年有2月29日,满足以下一个条件的年份为闰年: ...

  3. 2016年12月29日 星期四 --出埃及记 Exodus 21:24

    2016年12月29日 星期四 --出埃及记 Exodus 21:24 eye for eye, tooth for tooth, hand for hand, foot for foot,以眼还眼, ...

  4. 2016年11月29日 星期二 --出埃及记 Exodus 20:20

    2016年11月29日 星期二 --出埃及记 Exodus 20:20 Moses said to the people, "Do not be afraid. God has come t ...

  5. 2016年10月29日 星期六 --出埃及记 Exodus 19:14

    2016年10月29日 星期六 --出埃及记 Exodus 19:14 After Moses had gone down the mountain to the people, he consecr ...

  6. 2016年6月29日 星期三 --出埃及记 Exodus 14:26

    2016年6月29日 星期三 --出埃及记 Exodus 14:26 Then the LORD said to Moses, "Stretch out your hand over the ...

  7. [MySQL]--&gt;查询5天之内过生日的同事中的闰年2月29日问题的解决过程

    前言: 上次写了查询5天之内过生日的同事中的跨年问题的解决过程,网址为:http://blog.csdn.net/mchdba/article/details/38952033 ,当中漏了一个闰年2月 ...

  8. SQL点滴6—“微软不认识闰年2月29日”&字符"N"的作用

    原文:SQL点滴6-"微软不认识闰年2月29日"&字符"N"的作用 http://www.cnbeta.com/articles/50580.htm这个 ...

  9. MSDN i TELL YOU 又更新了,win10 1809版本的 3月29日的

    MSDN i TELL YOU 又更新了,1809版本的 3月29日的 WINDOWS 10 现在只有64位的 很好,估计 64位的普及了. 是一大改变

随机推荐

  1. usaco月赛,2017.1总结

    T1:跳舞的奶牛 大致题意:一个体积为k的舞台能够同时容纳k只奶牛一起跳舞,他们每头奶牛的跳舞时间不同,如果有一只奶牛跳完了第k+1头奶牛就会立刻上场跳舞,当所有奶牛跳完舞以后我们认为这次表演结束.现 ...

  2. 第一百二十一节,JavaScript事件绑定及深入

    JavaScript事件绑定及深入 学习要点: 1.传统事件绑定的问题 2.W3C事件处理函数 3.IE事件处理函数 4.事件对象的其他补充 事件绑定分为两种:一种是传统事件绑定(内联模型,脚本模型) ...

  3. UML(Unified Modeling Language)同一建模语言

    wiki定义: UML is a general-purpose, developmental, modeling language in the field of software engineer ...

  4. 初始化git

    git config --global user.name "Firstname Lastname" git config --global user.email "yo ...

  5. 拓展自定义编辑器窗口(EditorGUILayout类)

    Unity支持自行创建窗口,也支持自定义窗口布局.在Project视图中创建一个Editor文件夹,在文件夹中再创建一条脚本. 自定义窗口需要让脚本继承EditorWindow再设置MenuItem, ...

  6. ueditor1.4.3 在IE8下的 BUG

    ueditor1.4.3  .net 版 在IE8 下,多图片上传完成后,点击确认时报错,无法插入图片到编辑器中 原因是 ueditor.all.js 中的 24835 行 if (whitList[ ...

  7. 最近遇到的两个IE下的问题(IE兼容问题)

    最近遇到了两个IE下的兼容问题(产品目前还需要兼容IE8,所以没办法,运行效果虽然不好,但是也仍然兼容着吧) 问题描述: 1, 在更改IE窗口的时候,反应非常慢,甚至卡死 2, 在chrome运行正常 ...

  8. MVC教程

    http://developer.51cto.com/art/201309/409950_all.htm

  9. hibernate简单的增删改查

    获取当前线程的session protected Session getSession() { return sessionFactory.getCurrentSession(); } 增加:save ...

  10. Python--三元运算与lambda表达式

    三元运算: if 1 ==1: name = 'Tim' else: name = 'SB' 利用三元运算来完成上述4句语句任务: name = 'Tim' if 1==1 else 'SB' lam ...