时间限制: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. CODE[VS]-最小数和最大数-整数处理-天梯青铜

    题目描述 Description 输入n个数,n<=100,找到其中最小的数和最大的数 输入描述 Input Description 第一行一个整数n 接下来一行n个整数,每个整数不超过231 ...

  2. 导入spring源码到eclipse

    1.1安装Gradle 可以从http://www.gradle.org/downloads页面下载Gradle.下载后将文件解压到指定目录,我放在D:\软件\gradle-3.3,然后设置环境变量. ...

  3. PHP引用操作以及外部操作函数的局部静态变量的方法

    通过引用方式在外部操作函数或成员方法内部的静态变量 下面举个简单的例子,说明三个关于引用方面的问题: 1. 参数引用后函数内进行类型转换同样是地址操作 2. 参数引用后再传递给其他函数时需要再次添加引 ...

  4. GraphLab介绍[转]

    GraphLab介绍 原文链接:http://blog.jasonding.top/2015/06/08/Machine%20Learning/%E5%BC%80%E6%BA%90%E5%9B%BE% ...

  5. c# 操作xml之xmlReader

    xmlReader的名称空间using System.Xml; xmlReader是通过流的方式来读取xml文件的内容 <?xml version="1.0" encodin ...

  6. Chapter 21_1 字符串函数

    接下来开始接触Lua强大的字符串处理能功能——字符串库. 原始的Lua解释器操作字符串的能力很有限,真正强大的能力还是来自字符串库. 它所有的函数都在模块string中.它还为strings设置了一个 ...

  7. 关于MongoDB数据库中文件唯一性的问题

    ※重要※——介绍一下我的环境:MongoDB的“win32-x86_64-2008plus-ssl-3.0.5”,MongoVUE版本是1.6.9,VS2010,dll是1.10版本. MongoDB ...

  8. ListView 分页 排序、编辑、插入和删除

    摘自网络地址:http://msdn.microsoft.com/zh-cn/magazine/cc337984.aspx ListView 基础 ListView 是模板驱动的控件,这意味着它默认情 ...

  9. Angular DirtyChecking(脏值检查) $watch, $apply, $digest

    Dirty Checking (脏值检查) Digest cycle and $scope Digest cycle and $scope First and foremost, AngularJS ...

  10. linux开发

    linux开发资料 01 02 03 04 05 06 07 08 09 10 11 1 2 3 4 5 21 22 23 24 25