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位的普及了. 是一大改变
随机推荐
- [转]Numpy使用MKL库提升计算性能
from:http://unifius.wordpress.com.cn/archives/5 系统:Gentoo Linux (64bit, Kernel 3.7.1)配置:Intel(R) Cor ...
- 【安装】beautifulsoup4—美丽汤的安装
beautifulsoup俗称美丽汤,是用来爬虫用的,大家可以到这个网址去下载.注意,要根据对应的python版本 来下载. 下载传送: https://pypi.python.org/pypi/be ...
- C#里面Auotpostback回刷时候,textbox里面的password怎么保存
刷新页面时,如何保留页面上的PassWord模式下的TextBox的值? this.txtUserPwd.Attributes["value"] = txtUserPwd.Text ...
- Signal and Slots
Signal and Slots 用于对象之间通信. 它是 Qt 的核心特性之一, 并且也是Qt 与其它框架差别最大的部分. 概述 在GUI编程中, 如果我们改变了一个控件, 我们可能想其它控件知道: ...
- git 克隆到本地linux目录的2种方式
登录到gitlab查看2种不同的地址 ssh 类型 地址 git@inc.xxxx:shiwf/xxxAdmin.git http类型 地址 http://inc.xxxx:8000/shiwf/xx ...
- Elasticsearch相关配置(二)
一.关于elasticsearch的基本概念 term 索引词,在elasticsearch中索引词(term)是一个能够被索引的精确值.foo,Foo Foo几个单词是不相同的索引词.索引词(ter ...
- stm32
GPIO NVIC TIME USART ONE WIRE IIC SPI PWM ADC LCD XPT UCOSiii移植 定时器 蓝牙 陀螺仪
- String 类上的常用操作
java 中String 类上的常用操作: 首先创建对象 String line = new String("String demo"); String line2 = new ...
- iOS学习之Runtime(一)
一.Runtime简介 因为Objective-C是一门动态语言,所以它总是想办法把一些决定性工作从编译链接推迟到运行时,也就是说只有编译器是不够的,还需要一个运行时系统(runtime system ...
- use ContourPlot-使用ContourPlot
use ContourPlot to draw implicit function graphics 使用ContourPlot 画隐函数图像 for example $x^{3}+y^{3}-3xy ...