1185. 一周中的第几天
给你一个日期,请你设计一个算法来判断它是对应一周中的哪一天。输入为三个整数:day、month 和 year,分别表示日、月、年。您返回的结果必须是这几个值中的一个 {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}。来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/day-of-the-week
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
示例
示例 1:输入:day = 31, month = 8, year = 2019
输出:"Saturday"
示例 2:输入:day = 18, month = 7, year = 1999
输出:"Sunday"
示例 3:输入:day = 15, month = 8, year = 1993
输出:"Sunday"来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/day-of-the-week
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
提示:
- 给出的日期一定是在
1971
到2100
年之间的有效日期。
思路
蔡勒公式
代码
class Solution {
public:string dayOfTheWeek(int day, int month, int year) {//蔡勒公式vector<int> benchmark{0,3,2,5,0,3,5,1,4,6,2,4};vector<string>week={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};year-=month<3;return week[(year + year/4 - year/100 + year/400 + benchmark[month-1] + day) % 7];}
};