Given two dates, please calculate the number of February 29 between the two dates inclusively.
Only a leap year contains February 29. The year satisfying one of the following conditions is a leap year.
1. The year number can be divided by 4 but cannot be divided by 100.
2. The year number can be divided by 400.
InputThe first line contains an integer T, indicating the number of test cases.
Each test case contains two lines which are both in the format "Month Day, Year", indicating the dates. Month is a string contained in {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November" , "December"}. Day and Year are two integers.
We guarantee that all the dates are valid. The first date comes before the second date or the two dates are the same.
OutputFor each test case, output a line containing "Case #X: Y". X is the test case number starting from 1, and Y is the answer.
Sample Input4 January 12, 2012 March 19, 2012 August 12, 2899 August 12, 2901 August 12, 2000 August 12, 2005 February 29, 2004 February 29, 2012Sample Output
Case #1: 1 Case #2: 0 Case #3: 1 Case #4: 3
题意:两个日期间有多少个2-29
运用简单的容斥定理
闰年的个数:year/4+year/400-year/100;
#include<stdio.h>
#include<string.h>
char str[13][10]= {"\0","January","February", "March", "April","May",
"June", "July", "August","September", "October", "November", "December"};
int solve(int year)
{
return year/400+year/4-year/100;
}
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
int m1,m2,y1,y2,d1,d2,i;
char mon1[10],mon2[10];
scanf("%s %d,%d",mon1,&d1,&y1);
scanf("%s %d,%d",mon2,&d2,&y2);
for(i=0;i<=12;i++)
{
if(strcmp(mon1,str[i])==0)
m1=i;
if(strcmp(mon2,str[i])==0)
m2=i;
}
if(m1>2)
y1+=1;
if((m2<2)||(m2==2&&d2<29))
y2-=1;
printf("Case #%d: %d\n",cas++,solve(y2)-solve(y1-1));
}
return 0;
}