【每日一题12】AcWing 1341. 十三号星期五

Day12 AcWing 1341. 十三号星期五

思路

  1. 打表/枚举

拓展

  1. 基姆拉尔森计算公式(Kim larsen calculation formula)
  2. 闰年判断 year % 100 && year % 4 == 0 || year % 400 == 0

类似题目

  1. AcWing 1364. 序言页码
  2. AcWing 1568. 中文读数字
  3. AcWing 698. 读电话号码
  4. AcWing 436. 立体图

代码

按月份枚举
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // 打表
int weekday[7];


int main()
{
int n;
cin >> n;

int days = 0;
for (int year = 1900; year < 1900 + n; year ++ )
{
for (int i = 1; i <= 12; i ++ )
{
weekday[(days + 12) % 7] ++ ;
days += month[i];
if (i == 2)
{
if (year % 100 && year % 4 == 0 || year % 400 == 0)
days ++ ;
}
}
}

for (int i = 5, j = 0; j < 7; i = (i + 1) % 7, j ++ )
cout << weekday[i] << ' ';
cout << endl;

return 0;
}
按天枚举
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int months[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // 打表
int weekday[7];

int get(int year, int m)
{
if (m != 2) return months[m];
if (year % 100 && year % 4 == 0 || year % 400 == 0)
return 29;
return 28;
}

int main()
{
int n;
cin >> n;

int week = 0;
int year = 1900, month = 1, day = 1;
while (year < 1900 + n)
{
if (day == 13) weekday[week] ++ ;
week = (week + 1) % 7;
day ++ ;
if (get(year, month) < day) month ++, day = 1;
if (month > 12) month = 1, year ++ ;
}

for (int i = 5, j = 0; j < 7; i = (i + 1) % 7, j ++ )
cout << weekday[i] << ' ';
cout << endl;

return 0;
}