1185 LeetCode 一周中的第几天

题目描述:
LeetCode 第1185题 一周中的第几天
类型简单

思路:
利用蔡勒公式(Zeller)
该公式是一个计算星期的公式,随便给一个日期,就能用这个公式推算出是星期几

代码如下:

class Solution {
public:
    string dayOfTheWeek(int day, int month, int year) {
        string week[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
        if(month<3){
            year--;
            month+=12;
        }
        int y=year%100;
        int c=year/100;
        int m=month;
        int d=day;
        int res=y+y/4+c/4-2*c+(26*(m+1))/10+d-1;
        if(res>=0)   res=res%7;
        else res=(res%7+7)%7;
        return week[res];
    }
};
发布了224 篇原创文章 · 获赞 0 · 访问量 3144

猜你喜欢

转载自blog.csdn.net/peachzy/article/details/104875865