Hint:
- 每400年就有97个闰年
- 使用前缀和
- 起始日期大于2月,加一年
- 截止日期小于2月28/29号,减一年
#include <bits/stdc++.h>
using namespace std;
map<string,int> mp{{"January",1},{"February",2},{"March",3},{"April",4},{"May",5},{"June",6},{"July",7}
,{"August",8},{"September",9},{"October",10},{"November",11},{"December",12}};
bool isrun (int year)
{
return year%400==0||year%4==0&&year%100;
}
int getsum (int a)
{
int sum = a/400*97;
int tmp = a - a%400+1;
for (;tmp<=a;++tmp){
sum += isrun(tmp);
}
return sum;
}
int scan1 ()
{
string tmp;
int day,year,month;
cin>>tmp;
scanf("%d,%d",&day,&year);
month = mp[tmp];
if (month>2)
year++;
return year;
}
int scan2 ()
{
string tmp;
int day,year,month;
cin>>tmp;
scanf("%d,%d",&day,&year);
month = mp[tmp];
if (!(month>2||month==2&&(day==29||day==28)))
year--;
return year;
}
int main()
{
int t;
cin>>t;
for (int m=0;m<t;++m){
int a = scan1();
int b = scan2();
printf("case #%d:\n",m);
cout<<getsum(b)-getsum(a-1)<<endl;
}
}
看着map里的month有那么一瞬间我竟然觉得“月份”的英文不是month……