题目地址:https://train.usaco.org/usacoprob2?a=ddY7pfROLpX&S=friday。
或者我的OJ,http://47.110.135.197/problem.php?id=5185。
Is Friday the 13th really an unusual event?
That is, does the 13th of the month land on a Friday less often than on any other day of the week? To answer this question, write a program that will compute the frequency that the 13th of each month lands on Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday over a given period of N years. The time period to test will be from January 1, 1900 to December 31, 1900+N-1 for a given number of years, N. N is positive and will not exceed 400.
Note that the start year is NINETEEN HUNDRED, not NINETEEN NINETY.
There are few facts you need to know before you can solve this problem:
Do not use any built-in date functions in your computer language.
Don't just precompute the answers, either, please.
One line with the integer N.
20
Seven space separated integers on one line. These integers represent the number of times the 13th falls on Saturday, Sunday, Monday, Tuesday, ..., Friday.
36 33 34 33 35 35 34
就是输出从 1900 年 1 月 1 号(星期一)开始 1900+N-1 年 12 月 31 日期间,每月13号是星期几。
模拟。
由于数据范围小,本题最快的方法是打表。但是题目中有说到不要用打表,不知道用打表 OJ 服务器能否判断出,我估计是判断不了的,但是我没有测试过。
1、要注意闰年判断算法。
2、输出的时候格式。题目要求从星期六、星期天、星期一、......、星期五。
就是先按年循环,再按月循环。类似的伪码如下:
for (int year=0; year
/*
ID: your_id_here
PROG: friday
LANG: C++
*/
//Friday the Thirteenth
//https://train.usaco.org/usacoprob2?a=DzhbErzzOh0&S=friday
/*统计每月13号是星期几*/
#include
using namespace std;
//闰年判断
bool leapYear(int n) {
if ((0==n%4 && 0!=n%100) || (0==n%400)) {
return true;
} else {
return false;
}
}
int main() {
freopen("friday.in", "r", stdin);
freopen("friday.out", "w", stdout);
//读入n
int n;
cin >> n;
//1900年1月1日(星期一)统计到1900+N-1年12月31日。
//1900年1月13日为12天,12%7=5,为星期六
//1 2 3 4 5 6 7
//8 9 10 11 12 13
//星期统计,从星期六开始
const int days[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};//每月有几天
int ans[7] = {};
int gap = 12;//从1900年1月1日开始
for (int i=0; i