[模拟] aw3489. 星期几(模拟+日期问题+蔡勒公式+模板题)

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ypuyu

如果帮助到你,可以请作者喝水~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值