-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnineteen.cpp
More file actions
56 lines (46 loc) · 1.44 KB
/
Copy pathnineteen.cpp
File metadata and controls
56 lines (46 loc) · 1.44 KB
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>
#include <map>
using namespace std;
bool is_leap_year(int year) {
if (year % 100 == 0) {
return (year % 400 == 0);
}
return (year % 4 == 0);
}
// sunday = 0, monday = 1, ..., saturday = 6
// return number of sundays at the first of the month for that year
int sundays_per_year(int first_day_of_year, int year) {
map<int, int> days_in_month = {
{1, 31}, {2, 28}, {3, 31}, {4, 30},
{5, 31}, {6, 30}, {7, 31}, {8, 31},
{9, 30}, {10, 31}, {11, 30}, {12, 31}
};
int day_of_week = first_day_of_year;
int num_of_sundays = 0;
if (is_leap_year(year)) { days_in_month[2] = 29; }
for (int month = 1; month <= 12; month++) {
for (int day = 1; day <= days_in_month[month]; day++) {
if (day_of_week == 0 && day == 1) {
num_of_sundays++;
}
day_of_week++;
day_of_week %= 7;
}
}
return num_of_sundays;
}
int main() {
int num_of_sundays = 0;
int first_day_of_year = 2; // Jan 1, 1901
for (int yr = 1901; yr <= 2000; yr++) {
num_of_sundays += sundays_per_year(first_day_of_year, yr);
if (is_leap_year(yr)) {
first_day_of_year += 2;
} else {
first_day_of_year += 1;
}
first_day_of_year %= 7;
}
cout << "Number of total Sundays on the 1st of month: " << num_of_sundays << endl;
return 0;
}