forked from Suxyuuu/buaa-991
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2019_1.c
81 lines (73 loc) · 1.65 KB
/
2019_1.c
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* 求某年某月某日为星期几(已知1.1.1为周一) */
#include <stdio.h>
#include <windows.h>
int get_weekday(int year, int month, int day);
int main(void)
{
int year, month, day;
fputs("Date:", stdout);
scanf("%4d %2d %2d", &year, &month, &day);
printf("%d.%d.%d: ", year, month, day);
printf("%d\n", get_weekday(year, month, day));
return 0;
}
int get_weekday(int year, int month, int day)
{
// 1.1.1--Mon
// 1.2.1--Thu
// 2020.11.23--Mon
int day_sum = 0;
// 计算当年前n年的总天数
for (int i = 1; i < year; i++)
{
if (i % 400 == 0 || (i % 4 == 0 && i % 100 != 0))
{
day_sum += 366;
}
else
{
day_sum += 365;
}
}
// 计算当月前n月的天数(最后一个月直接加即可)
for (int i = 1; i < month; i++)
{
if (i == 2)
{
// 如果是闰年
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
{
day_sum += 29;
}
else
{
day_sum += 28;
}
}
else
{
switch (i)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day_sum += 31;
break;
case 4:
case 6:
case 9:
case 11:
day_sum += 30;
break;
default:
break;
}
}
}
day_sum += day;
return (day_sum % 7 == 0) ? 7 : (day_sum % 7);
}