LightOJ - 1414 February 29 (日期模拟)有多少个2月29

LightOJ - 1414
February 29
Time Limit:                                                        1000MS                        Memory Limit: 32768KB 64bit IO Format:                            %lld & %llu                       

SubmitStatus

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

Hint

Source

Problem Setter: Md. Arifuzzaman Arif
Special Thanks: Jane Alam Jan
//题意:
给你两个日期,让你找在这两个日期之间有几个2-29.
#include<cstdio>
#include<set>
#include<queue>
#include<cstring>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
bool js(int y,int r, int yy, int rr)
{
	if(y < yy)return true;
	if(y > yy)return false;
	if(r <= rr)return true;
	return false;
}
bool rn(int y)
{
	if(y % 400 == 0 || (y % 4 == 0 && y % 100 != 0))
		return true;
	return false;
}
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;
}
void work(int &y1,int &y2,int &m1,int &m2,int &d1,int &d2)
{
	if(y1 > y2)
	{
		swap(y1,y2);
		swap(m1,m2);
		swap(d1,d2);
		return;
	}
	if(y1 < y1)
	{
		return;
	}
	if(m1 > m2)
	{
		swap(y1,y2);
		swap(m1,m2);
		swap(d1,d2);
		return;
	}
	if(m1 < m2)
	{
		return;
	}
	if(d1 > d2)
	{
		swap(y1,y2);
		swap(m1,m2);
		swap(d1,d2);
		return;
	}
}
int find(int y)
{
	int t, t1, t2;
	t = (y - 2000) / 4 + 1;
	t1 = (y - 2000) / 100 + 1;
	t2 = (y - 2000) / 400 + 1;
	return (t - t1 + t2);
}
int main()
{
	int N, kase = 0;
	char s1[15], s2[15];
	int y1, d1, y2, d2, m1, m2;
	scanf("%d", &N);
	init();
	while(N--)
	{
		scanf("%s%d,%d", s1, &d1, &y1);
		scanf("%s%d,%d", s2, &d2, &y2);
		if(mp.count(s1))
		{
			m1 = mp[s1];
		}
		else while(1);
		if(mp.count(s2))
		{
			m2 = mp[s2];
		}
		else while(1);
		if(y1 == y2)
		{
			if(rn(y1) && js(m1,d1, 2, 29) && js(2, 29, m2, d2))
			{
				printf("Case %d: %d\n", ++kase, 1);
				continue;
			}
			printf("Case %d: %d\n", ++kase, 0);
			continue;
		}
		int t = find(y2 - 1) - find(y1);
		if(rn(y1) && js(m1,d1, 2, 29)) t++;
		if(rn(y2) && js(2, 29, m2, d2)) t++;
		printf("Case %d: %d\n", ++kase, t);
	}
	return 0;
}

你可能感兴趣的:(LightOJ - 1414 February 29 (日期模拟)有多少个2月29)