pku 3393 Lucky and Good Months by Gregorian Calendar 英语阅读题
哎。。怎么说呢,这种题目一点细节注意不到就是几个小时的浪费总结下题目吧
说白了就是计算公历系统中某年某月的第一天是不是周一、周六、周日以及某年某月的最后一天是不是周五、周六、周日
关于公历大家知道的就不说了,说几点大家可能不知道的
1、关于闰年
An year y, y > 1582 and y ≠ 1700, is a leap year if and only if(注意!1700算闰年)
-
y is divisible by 4, and
-
y is not divisible by 100 unless it is divisible by 400.
An year y, 0 < y < 1582 is a leap year if and only if
- y is divisible by 4.
11 days are eliminated starting September 3, 1752 in order for people not to rewrite history
下面就是常规的做法了。。
统计截止到每一年有多少天,然后将所有的lucky month和good month都找出来,顺序存放在数组中,查询的时候二分减一下就可以了~
代码:
1
# include
<
iostream
>
2 using namespace std;
3 int o[] = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 },l[] = { 31 , 29 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 };
4 int y[ 10001 ];
5 # include < vector >
6 # include < algorithm >
7 # define MON 730124
8 # define FRI 730121
9 # define SAT 730122
10 # define SUN 730123
11 vector < int > lucky,good;
12 int main()
13 {
14 // init
15 for ( int i = 1 ;i < 12 ;i ++ )
16 o[i] += o[i - 1 ],l[i] += l[i - 1 ];
17 y[ 0 ] = 0 ;
18 for ( int i = 1 ;i < 10000 ;i ++ )
19 {
20 if (i < 1582 && i % 4 == 0 || i > 1582 && (i % 4 == 0 && i % 100 != 0 || i % 400 == 0 ) || i == 1700 )
21 y[i] = y[i - 1 ] + l[ 11 ];
22 else
23 y[i] = y[i - 1 ] + o[ 11 ];
24 if (i == 1752 ) y[i] -= 11 ;
25 }
26 for ( int i = 1 ;i < 10000 ;i ++ )
27 {
28
29 int * m;
30 if (i < 1582 && i % 4 == 0 || i > 1582 && (i % 4 == 0 && i % 100 != 0 || i % 400 == 0 )) m = l;
31 else m = o;
32 if (i == 1752 )
33 {
34 for ( int j = 8 ;j < 12 ;j ++ ) m[j] -= 11 ;
35 }
36 if ((y[i - 1 ] + 1 - MON) % 7 == 0 || (y[i - 1 ] + 1 - SAT) % 7 == 0 || (y[i - 1 ] + 1 - SUN) % 7 == 0 ) good.push_back(i * 12 + 1 );
37 if ((y[i - 1 ] + o[ 0 ] - FRI) % 7 == 0 || (y[i - 1 ] + o[ 0 ] - SAT) % 7 == 0 || (y[i - 1 ] + o[ 0 ] - SUN) % 7 == 0 ) lucky.push_back(i * 12 + 1 );
38 for ( int j = 1 ;j < 12 ;j ++ )
39 {
40 if ((y[i - 1 ] + 1 + m[j - 1 ] - MON) % 7 == 0 || (y[i - 1 ] + 1 + m[j - 1 ] - SAT) % 7 == 0 || (y[i - 1 ] + 1 + m[j - 1 ] - SUN) % 7 == 0 ) good.push_back(i * 12 + j + 1 );
41 if ((y[i - 1 ] + m[j] - FRI) % 7 == 0 || (y[i - 1 ] + m[j] - SAT) % 7 == 0 || (y[i - 1 ] + m[j] - SUN) % 7 == 0 ) lucky.push_back(i * 12 + j + 1 );
42 }
43 if (i == 1752 )
44 {
45 for ( int j = 8 ;j < 12 ;j ++ ) m[j] += 11 ;
46 }
47 }
48 int n;
49 cin >> n;
50 while (n -- )
51 {
52 int y1,m1,y2,m2;
53 cin >> y1 >> m1 >> y2 >> m2;
54 cout << upper_bound(lucky.begin(),lucky.end(),y2 * 12 + m2) - lower_bound(lucky.begin(),lucky.end(),y1 * 12 + m1) << " " << upper_bound(good.begin(),good.end(),y2 * 12 + m2) - lower_bound(good.begin(),good.end(),y1 * 12 + m1) << endl;
55 }
56 // system("pause");
57 return 0 ;
58 }
59
2 using namespace std;
3 int o[] = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 },l[] = { 31 , 29 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 };
4 int y[ 10001 ];
5 # include < vector >
6 # include < algorithm >
7 # define MON 730124
8 # define FRI 730121
9 # define SAT 730122
10 # define SUN 730123
11 vector < int > lucky,good;
12 int main()
13 {
14 // init
15 for ( int i = 1 ;i < 12 ;i ++ )
16 o[i] += o[i - 1 ],l[i] += l[i - 1 ];
17 y[ 0 ] = 0 ;
18 for ( int i = 1 ;i < 10000 ;i ++ )
19 {
20 if (i < 1582 && i % 4 == 0 || i > 1582 && (i % 4 == 0 && i % 100 != 0 || i % 400 == 0 ) || i == 1700 )
21 y[i] = y[i - 1 ] + l[ 11 ];
22 else
23 y[i] = y[i - 1 ] + o[ 11 ];
24 if (i == 1752 ) y[i] -= 11 ;
25 }
26 for ( int i = 1 ;i < 10000 ;i ++ )
27 {
28
29 int * m;
30 if (i < 1582 && i % 4 == 0 || i > 1582 && (i % 4 == 0 && i % 100 != 0 || i % 400 == 0 )) m = l;
31 else m = o;
32 if (i == 1752 )
33 {
34 for ( int j = 8 ;j < 12 ;j ++ ) m[j] -= 11 ;
35 }
36 if ((y[i - 1 ] + 1 - MON) % 7 == 0 || (y[i - 1 ] + 1 - SAT) % 7 == 0 || (y[i - 1 ] + 1 - SUN) % 7 == 0 ) good.push_back(i * 12 + 1 );
37 if ((y[i - 1 ] + o[ 0 ] - FRI) % 7 == 0 || (y[i - 1 ] + o[ 0 ] - SAT) % 7 == 0 || (y[i - 1 ] + o[ 0 ] - SUN) % 7 == 0 ) lucky.push_back(i * 12 + 1 );
38 for ( int j = 1 ;j < 12 ;j ++ )
39 {
40 if ((y[i - 1 ] + 1 + m[j - 1 ] - MON) % 7 == 0 || (y[i - 1 ] + 1 + m[j - 1 ] - SAT) % 7 == 0 || (y[i - 1 ] + 1 + m[j - 1 ] - SUN) % 7 == 0 ) good.push_back(i * 12 + j + 1 );
41 if ((y[i - 1 ] + m[j] - FRI) % 7 == 0 || (y[i - 1 ] + m[j] - SAT) % 7 == 0 || (y[i - 1 ] + m[j] - SUN) % 7 == 0 ) lucky.push_back(i * 12 + j + 1 );
42 }
43 if (i == 1752 )
44 {
45 for ( int j = 8 ;j < 12 ;j ++ ) m[j] += 11 ;
46 }
47 }
48 int n;
49 cin >> n;
50 while (n -- )
51 {
52 int y1,m1,y2,m2;
53 cin >> y1 >> m1 >> y2 >> m2;
54 cout << upper_bound(lucky.begin(),lucky.end(),y2 * 12 + m2) - lower_bound(lucky.begin(),lucky.end(),y1 * 12 + m1) << " " << upper_bound(good.begin(),good.end(),y2 * 12 + m2) - lower_bound(good.begin(),good.end(),y1 * 12 + m1) << endl;
55 }
56 // system("pause");
57 return 0 ;
58 }
59