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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力的老周

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值