本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。
题目地址: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3876
思路
本题解题时用到了递推以及闰年的判断。五天假期与周末相连按假期开始的时间可分为以下几种情况:
(1)周一开始。假期前后各有两天周末假期,因此假期长度共九天。
(2)周二开始。假期后一天为周日,假期共六天。
(3)周日开始。假期前一天为周六,假期共六天。
(4)其他情况。假期共五天。
在公历中,若无闰年,特定日期所对应的星期每增加年会后推一天,闰年会再多后推一天。
如:2015年4月15日为周三, 闰年2016年4月15日为周五,2017年4月15日为周六。
查日历可知 1928 年 5 月 1 日为周二,利用for循环将1929年至9999年的5月1日的星期全部求出并存入数组,再通过前面得出的规律进行判断即可。
需要注意的地方
1.闰年所造成的星期偏差。
2.本题数据量较小,直接递推即可,无需额外计算。
Show me the code
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
int main()
{
int m, n, t;
int a[10005] = {0};
a[1928] = 2;
for(int i = 1929;i < 10000;i++)
{
a[i] = a[i - 1] + 1;
if((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) a[i]++;
}
while(cin >> t)
{
while(t--)
{
cin >> n;
if(a[n] % 7 == 0) cout << 6 << endl;
else if(a[n] % 7 == 1) cout << 9 << endl;
else if(a[n] % 7 == 2) cout << 6 << endl;
else cout << 5 << endl;
}
}
}