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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
USACO2022金组是国际在线判题系统USACO的最高级别,题目难度较高,在该比赛中取得好成绩是一项巨大的成就。以下是对该比赛的一些题目解析。 第一题:“交通计划” 题目要求:给定一个n个节点的有向图,每条边有一个长度,希望添加最少的边使得所有节点连通,求最小生成树的权值和。 解析:该题可以使用Kruskal算法求解,将每条边按权值从小到大排序,再依次加入,判断加入的边是否会形成环,若形成则不加入,直到所有节点连通为止。此时Kruskal算法得到的最小生成树的权值和即为所求。 第二题:“点火计划” 题目要求:给定一个n个节点的有向图,每条边有一个权值和一个点火时长,每个节点有一个点火启动时刻和时刻结束时刻,希望从其中选出一些边点火,使得所有节点都可从点火的边出发到达,且所选点火边的总点火时长最小。 解析:该题可以使用最小费用最大流算法求解。将每条边看做一个容量为1,费用为点火时长的边,源点向节点的点火边容量为1,费用为0的边,节点的点火边向汇点的容量为1,费用为0的边,对这个网络进行最小费用最大流即可得到所选边的总点火时长最小。 第三题:“美味佳肴” 题目要求:给定n个菜品,每个菜品有它的权值和两个类别,希望选出k个菜品,使得选出的菜品数量在每个类别中都不超过$\frac{k}{3}$个,且所选菜品的权值和最大。 解析:该题可以使用动态规划求解。设$f[i][j][k]$表示前i个菜品中,选择j个一类菜品,选择k个二类菜品的最大权值和,状态转移方程为$f[i][j][k]=max(f[i-1][j][k],f[i-1][j-1][k]+a[i],f[i-1][j][k-1]+b[i])$,其中a[i]为i号菜品的权值,若为一类则为该权值,否则为0,b[i]为i号菜品的权值,若为二类则为该权值,否则为0。最终答案为$f[n][$k/3$][$k/3$]。 以上是对USACO2022金组的部分题目的解析,USACO比赛是全球范围内的计算机竞赛,竞争非常激烈,能够在该比赛中脱颖而出是一项非常棒的成就。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

努力的老周

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

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

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

打赏作者

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

抵扣说明:

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

余额充值