Hihocoder 2月29日
描述
给定两个日期,计算这两个日期之间有多少个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: 3import 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日的更多相关文章
- 【hihoCoder】1148:2月29日
问题:http://hihocoder.com/problemset/problem/1148 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期). 思路: 1. 将问题转换成求两个日 ...
- hihoCoder 1148 2月29日
时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期). 只有闰年有2月29日,满足以下一个条件的年份为闰年: ...
- 2016年12月29日 星期四 --出埃及记 Exodus 21:24
2016年12月29日 星期四 --出埃及记 Exodus 21:24 eye for eye, tooth for tooth, hand for hand, foot for foot,以眼还眼, ...
- 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 ...
- 2016年10月29日 星期六 --出埃及记 Exodus 19:14
2016年10月29日 星期六 --出埃及记 Exodus 19:14 After Moses had gone down the mountain to the people, he consecr ...
- 2016年6月29日 星期三 --出埃及记 Exodus 14:26
2016年6月29日 星期三 --出埃及记 Exodus 14:26 Then the LORD said to Moses, "Stretch out your hand over the ...
- [MySQL]-->查询5天之内过生日的同事中的闰年2月29日问题的解决过程
前言: 上次写了查询5天之内过生日的同事中的跨年问题的解决过程,网址为:http://blog.csdn.net/mchdba/article/details/38952033 ,当中漏了一个闰年2月 ...
- SQL点滴6—“微软不认识闰年2月29日”&字符"N"的作用
原文:SQL点滴6-"微软不认识闰年2月29日"&字符"N"的作用 http://www.cnbeta.com/articles/50580.htm这个 ...
- MSDN i TELL YOU 又更新了,win10 1809版本的 3月29日的
MSDN i TELL YOU 又更新了,1809版本的 3月29日的 WINDOWS 10 现在只有64位的 很好,估计 64位的普及了. 是一大改变
随机推荐
- SQL总结之创建实例表空间监听
[创建数据库实例]cmd------>dbca[创建表空间-sql创建]create tablespace NSTC_WS logging datafile 'D:\app\dell\orada ...
- <hdu - 1272> 小希的迷宫 并查集问题 (注意特殊情况)
本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 Problem Description: 上次Gardon的迷宫城堡小希玩了很久(见Probl ...
- tableviewcell 点击 设置
table?.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) //设置cell 下边线 位置 table?.se ...
- Error C1189: #error: Please use the /MD switch for _AFXDLL builds
在VS 2013中编译程序时出现错误: 错误提示1: error C1189: #error : Building MFC application with /MD[d] (CRT dll versi ...
- wpf 遍历listview 时 传入指定类型 得到指定类型控件info
private ChildType FindVisualChild<ChildType>(DependencyObject obj) where ChildType : Dependenc ...
- anroid平台指纹方案
神盾的FingerPrint方案 在Java层,神盾主要提供如下几个包: egistec.fingerauth.api.FPAuthListeners; egistec.fingerauth.api. ...
- String to Double出现误差
场景描述 做实际项目的时候,由于使用Double类的valueOf得到一个用String类型保存的金额参数(单位为元),当需要转换成以分为单位即整形表示(Integer类表示)时,需要用之前得到的do ...
- 百度地图api写在html上可以实现,在jsp上会出现Bmap未定义的问题
在html上引用时用:<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0& ...
- Oracle SQL 关键字
1.UID返回标识当前用户的唯一整数SQL> show userUSER 为"GAO"SQL> select username,user_id from dba_use ...
- JavaScript DOM编程艺术-学习笔记(总结一)
1.1)dom-core方法:(不专属于js,支持dom的任何一种程序设计语言都可以使用它,它们的用途,也不仅限于处理网页,也可以用来处理任何一种标记语言编写处理的文档) ①getElementBy ...