lightoj-1414-February 29【容斥原理&&细节】

本文介绍了一种计算两个给定日期间闰日数量的方法,利用容斥原理判断闰年,并通过特殊案例分析调整结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:点击打开链接

1414 - February 29
Time Limit: 1 second(s)Memory Limit: 32 MB

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

Output for 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

Case 1: 1

Case 2: 0

Case 3: 1

Case 4: 3

Note

The names of the months are {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November" and "December"}. And the numbers of days for the months are {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 and 31} respectively in a non-leap year. In a leap year, number of days for February is 29 days; others are same as shown in previous line.


大意:找出给的两个日期之间有几个  2 月 29 日。

思路:容斥原理,中间的年数 4 的倍数减去 100 的倍数加上 400 的倍数,然后特判开始和结束的年和月,再对结果进行调整。

#include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long
using namespace std;
int day1,day2;
LL year1,year2;
char str1[20],month1[]="January";
char str2[20],month2[]="February";
bool judge(LL year)
{
	return year%(year%100?4:400)?0:1;
}
LL solve(LL x) // 容斥原理 
{
	return x/4-x/100+x/400;
}
bool check1(char str[],int day)
{
	if(!strcmp(str,month1)||(!strcmp(str,month2)&&day<=29))
		return 1;
	return 0;
}
bool check2(char str[],int day)
{
	if(!strcmp(str,month1)||(!strcmp(str,month2)&&day<29))
		return 1;
	return 0;
}
int main()
{
	int t,text=0;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%s%d,%lld",str1,&day1,&year1);
		scanf("%s%d,%lld",str2,&day2,&year2);
		LL ans=solve(year2)-solve(year1);
		if(judge(year1)&&check1(str1,day1))	ans++;
		if(judge(year2)&&check2(str2,day2))	ans--;
		printf("Case %d: %lld\n",++text,ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值