题解 | #Day of Week#
Day of Week
https://www.nowcoder.com/practice/a3417270d1c0421587a60b93cdacbca0
#include<iostream> #include<map> using namespace std; //一般都加上这句话 int mday[2][13] = { { 0,31,28,31,30,31,30,31,31,30,31,30,31 }, { 0,31,29,31,30,31,30,31,31,30,31,30,31 } }; string intToweekday[7] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday","Friday", "Saturday" }; map<string, int> monthtoint = { //根据key映射到对应的值 {"January",1}, {"February",2}, {"March",3}, {"April",4}, {"May",5}, {"June",6}, {"July",7}, {"August",8}, {"September",9}, {"October",10}, {"November",11}, {"December",12} }; bool isleap(int year) { if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) { return true; } else return false; } int main() { int year, day, mon; string month; bool isbefore; while (cin >> day >> month >> year) //输入数据 { int gap = 0; //与今天相差的天数 mon = monthtoint[month]; if (year < 2024 || year == 2024 && mon < 2 || year == 2024 && mon == 2 && day < 23) { isbefore = true; } else isbefore = false; int day1, mon1, year1, day2, mon2, year2; if (isbefore) { day1 = day; mon1 = mon; year1 = year; day2 = 23; mon2 = 2; year2 = 2024; } else { day2 = day; mon2 = mon; year2 = year; day1 = 23; mon1 = 2; year1 = 2024; } while (year1 * 10000 + mon1 * 100 + day1 < year2 * 10000 + mon2 * 100 + day2) { gap++; day1++; if (day1 == mday[isleap(year1)][mon1]) { day1 = 0; mon1++; if (mon1 == 13) { mon1 = 1; year1++; } } } cout << intToweekday[(gap+5)%7] << endl; } }