时间限制: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. Webupload + MVC 之上传下载

    最近工作中用到了 MVC 的上传及下载 , 写下感受 本项目中用到的是百度的webuploader <!--引入Jquery--> <script src="~/Conte ...

  2. Linux软件安装管理 - CentOS

    ---恢复内容开始--- 1. 软件包管理简介 1.1 源码包 - 脚本安装包 1.2 二进制包(RPM包,系统默认包) - 依赖性 2. rpm命令管理(Redhat Package Manager ...

  3. jQuery Validate【强大的表单验证】

    一.引入菜鸟教程提供的 1.14.0 版本下载地址:http://static.runoob.com/download/jquery-validation-1.14.0.zip <script ...

  4. 查找页面中最大的z-index 的值

    var divs = document.getElementsByTagName("div");for(var i=0, max=0; i<divs.length; i++) ...

  5. css 小知识

    <!-- IE下消除点击图片文字后出现的虚线框代码 --> <style type="text/css">a {blr:expression(this.on ...

  6. Ecstore安装篇-2.单机部署【linux】

    单机部署实施-linux 单机部署实施-linux author :James,jimingsong@vip.qq.com since :2015-03-02 系统环境需求 软件来源 底层依赖 1. ...

  7. 坑爹的 Hardware Reserved Memory (查看内存等)

    来源: http://blog.sina.com.cn/s/blog_772645ff0100s4t7.html 我的电脑是GatewayNV49C83c,酷睿i3的CPU和NV的独立显卡 Inter ...

  8. python 学习 异常处理

    异常处理实例 while True: num1 = input('num1:') num2 = input('num2:') try: num1 = int(num1) num2 = int(num2 ...

  9. java JDBC测试

    下载mysql-connector-java-5.1.31.jar添加到工程buildpath中 package com.jdbc.test; import java.sql.DriverManage ...

  10. 学习笔记(C++Primer)--易错点总结(Chapter2)

    2.1.2Type Conversions(1/10/2017) 1.If we assign an out-of-range value to an object of unsigned type, ...