MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / main

Function main

project_euler_problems/CountingSundays/solution.cpp:19–73  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17};
18
19int main()
20{
21 int total {0};
22
23 int year {1900};
24 int month {Jan};
25 int day {1};
26 int weekday {Mon};
27 int daysInMonth[12] {31, 28, 31, 30, 31, 30,
28 31, 31, 30, 31, 30, 31};
29
30 while (year < 2001){
31 // flip calendar to next year after Dec 31st
32 if (day == 32 && month == Dec){
33 year++;
34 day = 1;
35 month = Jan;
36
37 // on centuries divisible by 400 or non-centuries divisible
38 // by four, February has 29 days
39 if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)){
40 daysInMonth[Feb] = 29;
41 }
42 // on every other year it has 28
43 else {
44 daysInMonth[Feb] = 28;
45 }
46 }
47
48 // changes to the first of the next month
49 // if the day count is greater than allowed
50 // for the current month
51 if (day > daysInMonth[month]){
52 month++;
53 day = 1;
54 }
55
56 // the day after Sunday is set to Monday
57 if (weekday > Sun){
58 weekday = Mon;
59 }
60
61 // counts days after 1900 where Sunday is on the 1st
62 if (year > 1900 && weekday == Sun && day == 1){
63 total++;
64 }
65
66
67 // add one day to start the next loop
68 day++;
69 weekday++;
70 }
71
72 std::cout << "Total number of days counted: " << total << std::endl;
73}
74

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected