蓝桥杯 adv127 日期计算

蓝桥杯 adv127 日期计算

问题描述

已知2011年11月11日是星期五,问YYYY年MM月DD日是星期几?注意考虑闰年的情况。尤其是逢百年不闰,逢400年闰的情况。

输入格式

输入只有一行

YYYY MM DD

输出格式

输出只有一行

W

样例输入

2011 11 11

样例输出

5

数据规模和约定

1599 <= YYYY <= 2999 1 <= MM <= 12 1 <= DD <= 31,且确保测试样例中YYYY年MM月DD日是一个合理日期 1 <= W <= 7,分别代表周一到周日

参考解答

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>

using namespace std;

bool isLeapyear(int year);
int days(int year, int month, int day);
int distance(int year, int month, int day);

int main() {
// 2011-12-31 Saturday
int y, m, d;
cin >> y >> m >> d;
int distance1 = distance(y, m, d);
int case1[7] = { 6, 7, 1, 2, 3, 4, 5 };
int case2[7] = { 6, 5, 4, 3, 2, 1, 7 };
if (y >= 2012) {
cout << case1[distance1];
} else
cout << case2[distance1];

return 0;
}

bool isLeapyear(int year) {
return (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0));
}

int days(int year, int month, int day) {
int sum;

int months[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
sum = months[month-1] + day;
if (isLeapyear(year) && month > 2)
++sum;
return sum;
}

int distance(int year, int month, int day){
int distance1 = 0;
if (year >= 2012) {
distance1 = 365 * (year - 2012) + days(year, month, day);
for (int i = 2012; i < year; ++i) {
if (isLeapyear(i))
++distance1;
}
} else {
distance1 = 365 * (2012 - year) - days(year, month, day);
for (int i = year; i <= 2011; ++i) {
if (isLeapyear(i))
++distance1;
}
}

distance1 = distance1 % 7;
return distance1;
}

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×