【OJ】任意日期是星期几

#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
    string dayOfTheWeek(int day, int month, int year)
    {
        int num_of_day = GetDayBetweenYears(1971, year) + GetDayBetweenMonths(year, 1, month) + day - 1;
        return WEEKDAY[num_of_day % 7];
    }

private:
    int GetDayBetweenYears(const int first, const int second)
    {
        int sum = 0;
        for (int i = first; i < second; i++) {
            if (IsLeapYear(i)) {
                sum += days_leap;
            } else {
                sum += days_common;
            }
        }
        return sum;
    }
    int GetDayBetweenMonths(const int year, const int first, const int second)
    {
        int sum = 0;
        for (int i = first; i < second; i++) {
            sum += month_day[i];
        }
        if (second > 2 && IsLeapYear(year)) {
            sum += 1;
        }
        return sum;
    }
    bool IsLeapYear(const int i)
    {
        return (i % 400 == 0 || (i % 4 == 0 && i % 100 != 0));
    }
    constexpr static int epoch_year = 1971;
    constexpr static int epoch_mon = 1;
    constexpr static int epoch_day = 1;
    constexpr static int days_leap = 366;
    constexpr static int days_common = 365;
    vector<string> WEEKDAY{"Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"};
    vector<int> month_day{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
};
int main()
{
    int year = 0;
    int month = 0;
    int day = 0;
    cout << "Input year:" << endl;
    cin >> year;
    cout << "Input month:" << endl;
    cin >> month;
    cout << "Input day:" << endl;
    cin >> day;
    Solution a;
    cout << a.dayOfTheWeek(day, month, year);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值