基姆拉尔森计算公式

基姆拉尔森计算公式:给定年月日判断这一天是星期几

#include <iostream>
#include <string>
using namespace std;
string weekdays[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

// 返回0表示星期日,1表示星期一,依此类推
int get_week(int year, int month, int day) {
    //把一月和二月看成是上一年的十三月和十四月,
    //例:如果是2004-1-10则换算成:2003-13-10来代入公式计算。
    if(month == 1 || month == 2) {
        month = month == 1 ? 13 : 14;
        year -= 1;
    }
    int week = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400 + 1) % 7;
    return week;
};

int main() {
    int year, month, day;
    cin >> year >> month >> day;

    cout << weekdays[get_week(year, month, day)] << endl;
    return 0;
}

posted @ 2023-10-20 08:48  junlin623  阅读(31)  评论(0编辑  收藏  举报