1. 题目来源
链接:3489. 星期几
相关题目:
2. 题目解析
计算当前日期到 1 年 1 月 1 日之间的天数,模 7 即可。也就是基准值换算。
时间复杂度,3000 年,365 天,100 组数据,大于是 1 亿的计算量,但是常数比较小,可以过。
其中,判断平年、闰年函数很常用,得到某年某月的天数、每月的天数数组很常用!
可以将两个日期之间间隔多少天计算出来,可以封装为一个函数。
模拟:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <unordered_map>
using namespace std;
int months[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
unordered_map<string, int> month_name = {
{"January", 1},
{"February", 2},
{"March", 3},
{"April", 4},
{"May", 5},
{"June", 6},
{"July", 7},
{"August", 8},
{"September", 9},
{"October", 10},
{"November", 11},
{"December", 12}
};
string week_name[7] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
bool is_leap(int year) {
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
// 得到 xx年xx月的 天数
int get_days(int year, int month) {
int cnt = months[month];
if (month == 2) cnt += is_leap(year);
return cnt;
}
int main() {
int d, m, y;
string s;
while (cin >> d >> s >> y) {
int days = 0; // 总天数
m = month_name[s];
int i = 1, j = 1, k = 1; // 年-月-日
while (i < y || j < m || k < d) { // 顺序枚举即可
k ++, days ++ ;
if (k > get_days(i, j)) {
k = 1;
j ++ ;
if (j > 12) {
i ++ ;
j = 1;
}
}
}
cout << week_name[days % 7] << endl; // 周一: 0
}
return 0;
}
蔡勒公式代码:
#include <iostream>
#include <cstdio>
#include <vector>
#include <unordered_map>
using namespace std;
// 根据 年-月-日 通过蔡勒公式计算当前星期几
// 1: 星期一 ... 7: 星期日
int day_of_week(int year, int month, int day) {
if (month == 1 || month == 2) {
month += 12;
year -= 1;
}
int century = year / 100;
year %= 100;
int week = year + (year / 4) + (century / 4) - 2 * century + 26 * (month + 1) / 10 + day - 1;
week = (week % 7 + 7) % 7;
if (week == 0) week = 7;
return week;
}
vector<string> h{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
vector<string> M{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
unordered_map<string, int> s;
int main()
{
int d, y;
string m;
for (int i = 0; i < h.size(); i ++ ) s[h[i]] = i + 1;
while (cin >> d >> m >> y) {
int t = day_of_week(y, s[m], d);
cout << M[t - 1] << endl;
}
return 0;
}