[USACO1.1] 黑色星期五Friday the Thirteenth

题目描述

131313 号又是一个星期五,那么 131313号在星期五比在其他日子少吗?

为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数。给出 nnn 年的一个周期,要求计算 190019001900111111 日至 1900+n−11900+n-11900+n1121212313131 日中十三号落在周一到周日的次数。

这里有一些你要知道的:

  1. 190019001900111111 日是星期一。
  2. 4,6,114,6,114,6,11999 月有 303030 天,其他月份除了 222 月都有 313131 天,闰年 222 月有 292929 天,平年 222 月有 282828 天。
  3. 年份可以被 444 整除的为闰年(1992=4×4981992=4\times 4981992=4×498 所以 199219921992 年是闰年,但是 199019901990 年不是闰年)。
  4. 以上规则不适合于世纪年。可以被 400400400 整除的世纪年为闰年,否则为平年。所以,1700,1800,1900,21001700,1800,1900,21001700,1800,1900,2100 年是平年,而 200020002000 年是闰年。

输入格式

一个正整数 nnn

输出格式

依次输出周六、日、一、二、三、四、五在 131313 日出现的次数。

样例 #1

样例输入 #1

20

样例输出 #1

36 33 34 33 35 35 34

提示

【数据范围】
对于 100%100\%100% 的数据,1≤n≤4001\le n \le 4001n400

题目翻译来自NOCOW。

USACO Training Section 1.1

C++实现

#include
#include
#include <bits/stdc++.h>

using namespace std;

int week_day(int year,int month,int day){
if(month1||month2){
month+=12;
year–;
}
return (day+2month+3(month+1)/5+year+year/4
-year/100+year/400+1) % 7;
}

int main()
{
int year_len = 0;
cin >> year_len;
int count[7] = {0}; //what else can I explain/.
for (int current_year = 1900; current_year < 1900 + year_len; current_year++)
for (int current_month = 1; current_month <= 12; current_month++)
count[week_day(current_year, current_month, 13)]++;
cout << count[6] << " "<<count[0]<< " "<<count[1]<< " "<<count[2]
<< " "<<count[3]<< " "<<count[4]<< " "<<count[5];
return 0;
}

在这里插入图片描述

后续

接下来我会不断用C++来实现信奥比赛中的算法题、GESP考级编程题实现、白名单赛事考题实现,感兴趣的请关注,我后续将继续分享相关内容

Logo

2万人民币佣金等你来拿,中德社区发起者X.Lab,联合德国优秀企业对接开发项目,领取项目得佣金!!!

更多推荐