给定年月日计算星期几--Zeller‘s Congruence算法

C++代码如下,

class Solution {
public:
    string dayOfTheWeek(int day, int month, int year) {
        if (month == 1 || month == 2) {
            month += 12;
            year--;
        }

        int i = year / 100;
        int j = year % 100;
        
        int h = (day + 13 * (month + 1) / 5 + 5 * i + i / 4 + j + j / 4) % 7;

        string str[7] = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};

        return str[h];
    }
};

你可能感兴趣的:(C++学习,算法)