@@ -5,10 +5,18 @@ CS203_DSAA_template
55Copyright (C) 2020-2023 nanoseeds
66
77*/
8- #include " leetcode_1185_test.hpp"
8+ #ifdef CS203_DSAA_TEST_MACRO
9+
10+ #include < cstdint>
11+ #include < string>
12+ #include < array>
913#include < cassert>
1014
1115namespace leetcode_1185 {
16+ using std::string;
17+
18+ inline constexpr const std::array<const char *const , 7 > weekStrs
19+ {" Sunday" , " Monday" , " Tuesday" , " Wednesday" , " Thursday" , " Friday" , " Saturday" };
1220
1321int32_t getYearDays (int32_t year) {
1422 assert (1971 <= year & year <= 2100 );
@@ -30,29 +38,35 @@ int32_t getMonthDays(int32_t year, int32_t month) {
3038 // 7*31+5*30+28=187+180+28=365
3139 return monthDays[month];
3240}
41+ #endif
3342
34- string leetcode_1185::dayOfTheWeek (int day, int month, int year) {
35- // 1993 08 15 是sunday周日
36- // 1993 08 01 是周日
37- // 1993 01 01 间隔 31+28+31+30+31+30+31= 212天 %7 == 2天,因此是周五
38- // 1971 01 01 间隔 22年,其中有1972,1976,1980,1984,1988,1992六个闰年,所以是22*365+6=7300+730+6=8036天 % 7 = 336 % 7 = 56 % 7 = 0,所以也是周五
39- const auto yearDays{[](const auto lastYear) {
40- int32_t will_return{0 };
41- for (auto begin{1971 }; begin < lastYear; ++begin) {
42- will_return += getYearDays (begin);
43- }
44- return will_return;
45- }(year)};
46- const auto monthDays{[](const auto lastMonth, const auto year) {
47- int32_t will_return{0 };
48- for (auto begin{1 }; begin < lastMonth; ++begin) {
49- will_return += getMonthDays (year, begin);
50- }
51- return will_return;
52- }(month, year)};
53- const auto daysDiff{day - 1 };
54- const auto choice{(yearDays + monthDays + daysDiff + 5 ) % 7 };
55- return weekStrs[choice];
56- }
43+ class Solution {
44+ public:
45+ string dayOfTheWeek (int day, int month, int year) {
46+ // 1993 08 15 是sunday周日
47+ // 1993 08 01 是周日
48+ // 1993 01 01 间隔 31+28+31+30+31+30+31= 212天 %7 == 2天,因此是周五
49+ // 1971 01 01 间隔 22年,其中有1972,1976,1980,1984,1988,1992六个闰年,所以是22*365+6=7300+730+6=8036天 % 7 = 336 % 7 = 56 % 7 = 0,所以也是周五
50+ const auto yearDays{[](const auto lastYear) {
51+ int32_t will_return{0 };
52+ for (auto begin{1971 }; begin < lastYear; ++begin) {
53+ will_return += getYearDays (begin);
54+ }
55+ return will_return;
56+ }(year)};
57+ const auto monthDays{[](const auto lastMonth, const auto year) {
58+ int32_t will_return{0 };
59+ for (auto begin{1 }; begin < lastMonth; ++begin) {
60+ will_return += getMonthDays (year, begin);
61+ }
62+ return will_return;
63+ }(month, year)};
64+ const auto daysDiff{day - 1 };
65+ const auto choice{(yearDays + monthDays + daysDiff + 5 ) % 7 };
66+ return weekStrs[choice];
67+ }
68+ };
5769
70+ #ifdef CS203_DSAA_TEST_MACRO
5871}
72+ #endif
0 commit comments