【Light OJ 1414】February 29(容斥原理)

Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu

Submit Status

Description

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

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.

Output


For each case, print the case number and the number of leap days in between two given dates (inclusive).

Sample Input

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

Sample Output

Case 1: 1

Case 2: 0

Case 3: 1

Case 4: 3

容斥原理求該年之前閏年的個數

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<map>
#include<algorithm>
using namespace std;
map<string,int>mp;
void init() {
	int flag=1;
	mp["January"]=flag++;
	mp["February"]=flag++;
	mp["March"]=flag++;
	mp["April"]=flag++;
	mp["May"]=flag++;
	mp["June"]=flag++;
	mp["July"]=flag++;
	mp["August"]=flag++;
	mp["September"]=flag++;
	mp["Ocober"]=flag++;
	mp["November"]=flag++;
	mp["December"]=flag++;
}
bool judge(int n) {
	if(n%400==0||n%4==0&&n%100!=0)
		return true;
	return false;
}
int main() {
	int T,casen=0,ans;
	char c1[20],c2[20];
	int year1,year2,day1,day2;
	scanf("%d",&T);
	init();
	while(T--) {
		int ans=0;
		scanf("%s%d,%d",c1,&day1,&year1);
		scanf("%s%d,%d",c2,&day2,&year2);
		int cnt1,cnt2;
		cnt1=year1/4-year1/100+year1/400;                //容斥原理
		cnt2=year2/4-year2/100+year2/400;
		if(judge(year1)){
			if(mp[c1]==1||mp[c1]==2){		//只有月份大於2時year1年之前的2.29個數纔不減 
				cnt1--;
			}
		}
		if(judge(year2)){
			if(mp[c2]==1||mp[c2]==2&&day2<=28){
				cnt2--;
			}
		}
		ans=cnt2-cnt1;
		printf("Case %d: %d\n",++casen,ans);
	}
	return 0;
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章