Skip to content

Commit 0f5ef95

Browse files
authored
Create zeller-day-of-week-algorithm.cpp
1 parent 678f912 commit 0f5ef95

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

zeller-day-of-week-algorithm.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
bool isLeap(int year) {
3+
return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
4+
}
5+
const string week[7] = {"Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday"};
6+
public:
7+
string dayOfTheWeek(int day, int month, int year) {
8+
int days = 0;
9+
for (int i = 1971; i < year; i++) {
10+
if (isLeap(i)) {
11+
days += 366;
12+
} else {
13+
days += 365;
14+
}
15+
}
16+
for (int i = 1; i < month; i++) {
17+
if (i == 2) {
18+
if (isLeap(year)) {
19+
days += 29;
20+
} else {
21+
days += 28;
22+
}
23+
}
24+
else if (i == 4 || i == 6 || i == 9 || i == 11) days += 30;
25+
else days += 31;
26+
}
27+
days += day;
28+
return week[days % 7];
29+
}
30+
};

0 commit comments

Comments
 (0)