W - February 29(容斥原理)

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 between2 * 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

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.



emmmm要注意的时候暴力一定会T

所以才用离散数学的容斥

但是在处理边界的时候会有问题所以这边要格外注意

我是看博客写的解释代码上有

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
char mon[3][15];
char what[15][15]= {" ","January", "February", "March","April", "May", "June",
 "July", "August", "September", "October", "November","December"};
int day[3];
int month[3];
int year[3];
//四年一闰百年不闰四百年又闰  
bool judge(int x)
{
	if(x%400==0||x%100!=0&&x%4==0)
	return true;
	return false;	
}//判断是闰年还是平年的函数 
int main()
{
	int t;
	scanf("%d",&t);
     for(int k=1;k<=t;k++)
    {
    	scanf("%s %d, %d",mon[1],&day[1],&year[1]);
    	scanf("%s %d, %d",mon[2],&day[2],&year[2]);
    	for(int i=1;i<=12;i++)
    	{
    		if(strcmp(mon[1],what[i])==0)
    		{
    			month[1]=i;
    			break; 
    		}
    	}
    	for(int i=1;i<=12;i++)
    	{
    		if(strcmp(mon[2],what[i])==0)
    		{
    			month[2]=i;
    			break; 
    		}
    	}
        if(month[1]>2)  
        year[1]++;//因为比2月大所以不管是平年还是闰年都可以不用管因为这个都不算  都可以直接当作下一年的1.1 
        if(month[2]<2||month[2]==2&&day[2]<29)  
        year[2]--; //如果比2月29号小的话 那也可以减少因为不管啊是平年还是闰年都无所谓了 都可以直接当作上一年的12.31 
		int leap1=(year[1]-1)/4-(year[1]-1)/100+(year[1]-1)/400;
		
		/*
		减一是为了当第一个年份是闰年的时候  这样子就不会减掉了这个
		而第一个年份不是闰年的时候 随意减掉都无所谓  因闰年的个数都不变             
		year[1]-1的原因是因为 假设都是 2012年的时候  都是 488那么直接给减掉了等于0  是错误的 
		为2005减掉一之后是2004是486个
	    我觉得这个算是避免相同吧 
		*/
        int leap2=year[2]/4-year[2]/100+year[2]/400;  
        int ans=leap2-leap1;  
        printf("Case %d: %d\n",k,ans);    
    }
	return 0;
}











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值