-
Notifications
You must be signed in to change notification settings - Fork 0
/
1185. Day of the Week.c
41 lines (35 loc) · 1.06 KB
/
1185. Day of the Week.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
int leap(int year);
char * dayOfTheWeek(int day, int month, int year){
int time = 0;
int monthTime[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char *returnDay[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
for (int i = 1971; i < year; i++) {
if (leap(i)) {
time += 366;
}else {
time += 365;
}
}
for (int i = 0; i < month - 1; i++) {
time += monthTime[i];
}
if (month > 2 && leap(year))
time++;
time += day;
time = time % 7;
printf("%d ", time);
return returnDay[(time + 4) % 7];
}
int leap(int year)
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return 1;
}else {
return 0;
}
}
/**
Given a date, return the corresponding day of the week for that date.
The input is given as three integers representing the day, month and year respectively.
Return the answer as one of the following values {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}.
*/