It is 2012, and it's a leap year. So there is a "February 29" in this year, which is called leap day. Interesting thing is the infant who will born in this February 29, will get his/her birthday again in 2016, which is another leap year. So February 29 only exists in leap years. Does leap year comes in every 4 years? Years that are divisible by 4 are leap years, but years that are divisible by 100 are not leap years, unless they are divisible by 400 in which case they are leap years.
In this problem, you will be given two different date. You have to find the number of leap days in between them.
Input starts with an integer T (≤ 550), denoting the number of test cases.
Each of the test cases will have two lines. First line represents the first date and second line represents the second date. Note that, the second date will not represent a date which arrives earlier than the first date. The dates will be in this format - "month day, year", See sample input for exact format. You are guaranteed that dates will be valid and the year will be in between 2 * 103 to 2 * 109. For your convenience, the month list and the number of days per months are given below. You can assume that all the given dates will be a valid date.
For each case, print the case number and the number of leap days in between two given dates (inclusive).
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
在时间推算的问题上其实很容易想到用计数器来实现,但是还有一种思路就是用如下思路
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
struct node{
char month[15];
ll day,year;
};
map<string,int> mp;
void init(){
mp["January"]=1;
mp["February"]=2;
mp["March"]=3;
mp["April"]=4;
mp["May"]=5;
mp["June"]=6;
mp["July"]=7;
mp["August"]=8;
mp["September"]=9;
mp["October"]=10;
mp["November"]=11;
mp["December"]=12;
}
bool cmp(node a,node b){
if(a.year==b.year){
if(mp[a.month]==mp[b.month]){
return a.day<b.day;
}
return mp[a.month]<mp[b.month];
}
return a.year<b.year;
}
bool judge(ll year){
if(year%4==0&&year%100!=0||year%400==0)return true;
return false;
}
int main(){
node first,second;
ll n;
init();
int cnt=0;
scanf("%lld",&n);
getchar();
while(n--){
scanf("%s %lld, %lld",first.month,&first.day,&first.year);
getchar();
scanf("%s %lld, %lld",second.month,&second.day,&second.year);
getchar();
if(!cmp(first,second)){
node t=first;
first=second;
second=t;
}
ll aa=first.year/4-first.year/100+first.year/400;//小于第一年的所有闰年个数
ll bb=second.year/4-second.year/100+second.year/400;//小于第二年的所有闰年的个数
if(judge(first.year)&&judge(second.year)){//都是闰年时.
if(mp[first.month]>2);//如果第一年的月份大于2,什么也不做,因为已经计入
else aa--;//否则计数器减一
if(mp[second.month]>2||mp[second.month]==2&&second.day==29);
else bb--;
}
else if(!judge(first.year)&&judge(second.year)){
if(mp[second.month]>2||mp[second.month]==2&&second.day==29);
else bb--;
}
else if(judge(first.year)&&!judge(second.year)){
if(mp[first.month]<=2)aa--;
}
printf("Case %d: %d\n",++cnt,bb-aa);
}
return 0;
}