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

题目描述

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

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

这里有一些你要知道的:

  1. 1900 1900 1900 1 1 1 1 1 1 日是星期一。
  2. 4 , 6 , 11 4,6,11 4,6,11 9 9 9 月有 30 30 30 天,其他月份除了 2 2 2 月都有 31 31 31 天,闰年 2 2 2 月有 29 29 29 天,平年 2 2 2 月有 28 28 28 天。
  3. 年份可以被 4 4 4 整除的为闰年( 1992 = 4 × 498 1992=4\times 498 1992=4×498 所以 1992 1992 1992 年是闰年,但是 1990 1990 1990 年不是闰年)。
  4. 以上规则不适合于世纪年。可以被 400 400 400 整除的世纪年为闰年,否则为平年。所以, 1700 , 1800 , 1900 , 2100 1700,1800,1900,2100 1700,1800,1900,2100 年是平年,而 2000 2000 2000 年是闰年。

输入格式

一个正整数 n n n

输出格式

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

样例 #1

样例输入 #1

20

样例输出 #1

36 33 34 33 35 35 34

提示

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

题目翻译来自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,联合德国优秀企业对接开发项目,领取项目得佣金!!!

更多推荐