题目链接:http://hihocoder.com/contest/msbop2015qual/problems
A. 2月29日
首先为了方便处理可以分类一下把问题转化成第a年到第b年的闰年数量。
然后如果要求fun(x)=1~x年中闰年的数量,容斥一下就是 (1~x中4的倍数) - (1~x中100的倍数) + (1~x中400的倍数)
答案为fun(b) - fun(a-1)。
代码(1ms):
1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <iostream> 5 using namespace std; 6 7 string month[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November" , "December"}; 8 int T, n; 9 10 int get_month(string s) { 11 return find(month, month + 12, s) - month + 1; 12 } 13 14 int calc(int y) { 15 return y / 4 - y / 100 + y / 400; 16 } 17 18 char str[15]; 19 int yy, dd, mm; 20 21 int main() { 22 scanf("%d", &T); 23 for(int t = 1; t <= T; ++t) { 24 scanf("%s %d, %d", str, &dd, &yy); 25 mm = get_month(str); 26 int a = calc(yy + (mm > 2) - 1); 27 28 scanf("%s %d, %d", str, &dd, &yy); 29 mm = get_month(str); 30 int b = calc(yy - (mm < 2 || (mm == 2 && dd < 29))); 31 printf("Case #%d: %d\n", t, b - a); 32 } 33 }