题目

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:

• January 1, 1900 was on a Monday.
• Thirty days has September, April, June, and November, all the rest have 31 except for February which has 28 except in leap years when it has 29.
• Every year evenly divisible by 4 is a leap year (1992 = 4*498 so 1992 will be a leap year, but the year 1990 is not a leap year)
• The rule above does not hold for century years. Century years divisible by 400 are leap years, all other are not. Thus, the century years 1700, 1800, 1900 and 2100 are not leap years, but 2000 is a leap year.

Do not use any built-in date functions in your computer language.

INPUT FORMAT

One line with the integer N.

20

OUTPUT FORMAT

Seven space separated integers on one line. These integers represent the number of times the 13th falls on Saturday, Sunday, Monday, Tuesday, ..., Friday.

SAMPLE OUTPUT (file friday.out)

36 33 34 33 35 35 34

题目分析

数据范围

$1 \leqslant N \leqslant 400$

坑点

1、要注意闰年判断算法。

2、输出的时候格式。题目要求从星期六、星期天、星期一、......、星期五。

算法思路

for (int year=0; year<n; year++) {   /*年循环*/
for (int month=1; month<=12; month++) {    /*月循环*/
do something
}
}

AC参考代码

/*
ID: your_id_here
PROG: friday
LANG: C++
*/
//Friday the Thirteenth
//https://train.usaco.org/usacoprob2?a=DzhbErzzOh0&S=friday
/*统计每月13号是星期几*/
#include <bits/stdc++.h>
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<n; i++) {
//年
for (int j=1; j<=12; j++) {
//月
//计算到上月13日，差几天
gap += days[j-1];
if (3==j && true==leapYear(1900+i)) {
//闰年2月多一天
gap++;
}
ans[gap%7]++;
}
gap += 31;
}

//输出
cout << ans[5] << " " << ans[6];
for (int i=0; i<5; i++) {
cout << " " << ans[i];
}
cout << endl;

fclose(stdin);
fclose(stdout);

return 0;
}

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客