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

P1202题库链接:https://www.luogu.org/problem/P1202

难度:普及-

算法标签:模拟,数论

1.朴素模拟

本题使用数组和map来解都行,由于最近用map比较多,所以我使用map来解,建立一个map容器来记录每月13号对应星期出现的次数,并定义一个变量k来模拟每星期中的每一天,将平年中1到12月的天数存在date数组中,若第i年为闰年,则只需二月循环29次,其他与平年一样,按date数组循环即可,从1900年开始重复n次,最后输出map容器中周一到周日所对应的次数

 1 #include <cstdio>
 2 #include <map>
 3 using namespace std;
 4 map<int, int> m;
 5 int date[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 6 int main()
 7 {
 8     int n, week = 1;
 9     scanf("%d", &n);
10     for(int i = 1900; i <= 1900 + n - 1; ++i)
11     {
12         for(int j = 0; j <= 11; ++j)
13         {
14             if(j == 1)
15             {
16                 if(i % 400 == 0)
17                 {
18                     for(int k = 1; k <= 29; ++k)
19                     {
20                         if(week - 1 == 7) week = 1;
21                         if(k == 13) ++m[week];
22                         ++week;
23                     }
24                     ++j;
25                 }
26                 else if(i % 100 != 0 && i % 4 == 0)
27                 {
28                     for(int k = 1; k <= 29; ++k)
29                     {
30                         if(week - 1 == 7) week = 1;
31                         if(k == 13) ++m[week];
32                         ++week;
33                     }
34                     ++j;
35                 }
36             }
37             for(int k = 1; k <= date[j]; ++k)
38             {
39                 if(week - 1 == 7) week = 1;
40                 if(k == 13) ++m[week];
41                 ++week;
42             }
43         }
44     }
45     printf("%d %d %d %d %d %d %d", m[6], m[7], m[1], m[2], m[3], m[4], m[5]);
46     return 0;
47 }
上一篇:Java - Enum 枚举


下一篇:JavaScript(jQuery)实现打印英文格式日期