题解 | #Day of Week#

Day of Week

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

#include <iostream>
#include <cstdio>
#include <string>
#include <map>

using namespace std;


int main() {
  	//数字与星期几的映射
    string intToWeekday[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    //月份与int的映射
    map<string, int> monthToInt = {
        {"January",   1},
        {"February",  2},
        {"March",     3},
        {"April",     4},
        {"May",       5},
        {"June",      6},
        {"July",      7},
        {"August",    8},
        {"September", 9},
        {"October",   10},
        {"November",  11},
        {"December",  12}
    };
    int mDay[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int year, mon, day;
    char str[100];
    string month;
    bool isBefore;
    //9 October 2001
    //scanf用C风格
    while (scanf("%d%s%d", &day, str, &year) != EOF) {
        month = str;//把字符串从C风格转换成C++风格
        mon = monthToInt[month]; //从字符串到整数
        //判断是今天以前还是以后
        if (year < 2023 || 2023 == year && mon < 2 || 2023 == year && 2 == mon &&
                day < 23) {
            isBefore = true;
        } else {
            isBefore = false;
        }
        //从begin走到end
        int begYear, begMon, begDay, endYear, endMon, endDay;
        if (isBefore) {
            begYear = year;
            begMon = mon;
            begDay = day;
            endYear = 2023;
            endMon = 2;
            endDay = 23;
        } else {
            begYear = 2023;
            begMon = 2;
            begDay = 23;
            endYear = year;
            endMon = mon;
            endDay = day;
        }
        //算从起点到终点有多少天
        int totalDay = 0;
        while (true) {
            if (begDay == endDay && begMon == endMon && begYear == endYear) {
                break;
            }
            totalDay++;
            bool isLeap = begYear % 400 == 0 || begYear % 4 == 0 && begYear % 100 != 0;
            if (isLeap) {
                mDay[2] = 29;
            } else {
                mDay[2] = 28;
            }
            begDay++;
            if (begDay > mDay[begMon]) {
                begDay = 1;
                ++begMon;
                if (begMon > 12) {
                    begMon = 1;
                    ++begYear;
                }
            }
        }
        if (isBefore) {
            printf("%s\n", intToWeekday[(11 - totalDay % 7) % 7 ].c_str());
        } else {
            printf("%s\n", intToWeekday[(totalDay + 4) % 7 ].c_str());
        }

    }


}

全部评论

相关推荐

一个菜鸡罢了:哥们,感觉你的简历还是有点问题的,我提几点建议,看看能不能提供一点帮助 1. ”新余学院“别加粗,课程不清楚是否有必要写,感觉版面不如拿来写一下做过的事情,教育经历是你的弱势就尽量少写 2. “干部及社团经历”和“自我评价”删掉 3. 论文后面的“录用”和“小修”啥的都删掉,默认全录用,问了再说,反正小修毕业前肯定能发出来 4. 工作经验和研究成果没有体现你的个人贡献,着重包装一下个人贡献
点赞 评论 收藏
分享
11-11 14:21
西京学院 C++
Java抽象练习生:教育背景放最前面,不要耍小聪明
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务