1971-2100年之间都是四年一闰,没有400的倍数。1972年是闰年,但是该题算法是计算当年之前的天数,再加上当年的天数,再加上当年是否是闰年的一天,所以到1973年才能加上贡献的天数,所以是1973-4=1969
class Solution {
public:
string dayOfTheWeek(int day, int month, int year) {
vector<string> wed = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
vector<int> months = {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 += months[i];
}
if(((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) && month >= 3) {
days += 1;
}
days += day;
return wed[(days + 3) % 7];
}
};
class Solution {
public:
string dayOfTheWeek(int day, int month, int year) {
tm t = {0, 0, 0, day, month - 1, year - 1900};
const vector<string> weds = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
// 转换为time_t类型的UTC时间
time_t dt = mktime(&t);
return weds[localtime(&dt)->tm_wday];
}
};
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!