P1202 [USACO1.1] Black Friday Friday the Thirteenth

P1202 exam Links: https://www.luogu.org/problem/P1202

Difficulty: universal -

Algorithms Tags: analog, number theory

1. simple simulation

This title uses an array and map to solve all right, due to the recent use map more, so I use the map to solve, build a map container to record the number of 13 per month corresponding to the week appear, and define a variable k to simulate every week every day, the number of days in leap year 1-12 months of existence date array, if i year is a leap year, only need to cycle 29 times in February, with average year, like the other, according to the array cycle to date, from 1900 repeated n times, the last number of outputs container map corresponding to Monday to Sunday

 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 }

Guess you like

Origin www.cnblogs.com/ZhangRunqi/p/11294478.html