USACO题解——Section 1.2——Friday the Thirteenth

题目地址: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:

  • 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.

Don't just precompute the answers, either, please.

PROGRAM NAME: friday

INPUT FORMAT

One line with the integer N.

SAMPLE INPUT (file friday.in)

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

题目分析

题目意思

就是输出从 1900 年 1 月 1 号(星期一)开始 1900+N-1 年 12 月 31 日期间,每月13号是星期几。

算法归类

模拟。

数据范围

1 \leqslant N \leqslant 400

由于数据范围小,本题最快的方法是打表。但是题目中有说到不要用打表,不知道用打表 OJ 服务器能否判断出,我估计是判断不了的,但是我没有测试过。

坑点

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;
}
发布了138 篇原创文章 · 获赞 7 · 访问量 41万+

猜你喜欢

转载自blog.csdn.net/justidle/article/details/103983567