题解 | #Day of Week#

Day of Week

https://www.nowcoder.com/practice/a3417270d1c0421587a60b93cdacbca0

#include <iostream>
#include <map>
using namespace std;

bool is_run(int y) {
    if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) return true;
    return false;
}

int main() {
    int d, y;
    string month;
    map<string, int> m;
    m["January"] = 1, m["February"] = 2, m["March"] = 3, m["April"] = 4,
    m["May"] = 5, m["June"] = 6, m["July"] = 7, m["August"] = 8,
    m["September"] = 9, m["October"] = 10, m["November"] = 11, m["December"] = 12;
    int month_day[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    string res[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
    while (cin >> d >> month >> y) {
        int end_day = 0;
        for (int i = 1; i < y; i++) {
            if (is_run(i)) end_day += 366;
            else end_day += 365;
        }
        for (int i = 1; i < m[month]; i++) {
            if (i == 2) {
                if (is_run(y)) end_day += 29;
                else end_day += 28;
            } else {
                end_day += month_day[i];
            }
        }
        end_day += d;
        end_day -= 1;
        int k = end_day % 7;
        cout << res[k] << endl;
    }
    return 0;
}

全部评论

相关推荐

oppo 应用软开 22*15+0.5*12
拿到了ssp完美:真的坎坷,但是你至少拿到这么多offer了!
点赞 评论 收藏
分享
尊嘟假嘟点击就送:加v细说,问题很大
点赞 评论 收藏
分享
死在JAVA的王小美:哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈,我也是,让我免了一轮,但是硬气拒绝了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
11-27 10:46
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务