直接计算法,已知一个1970年12月31日是星期三,计算当前日期到给定日期的天数,然后计算出星期。
class Solution {
public:
string dayOfTheWeek(int day, int month, int year) {
vector<string> week = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
vector<int> monthDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30};
/* 输入年份之前的年份的天数贡献 */
int days = 365 * (year - 1971) + (year - 1969) / 4;
/* 输入年份中,输入月份之前的月份的天数贡献 */
for (int i = 0; i < month - 1; ++i) {
days += monthDays[i];
}
if ((year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) && month >= 3) {
days += 1;