#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;
}
标签:星期,OJ,int,sum,month,year,31,任意,day
From: https://blog.csdn.net/CSUzhangpeike/article/details/136659561