题解 | #Day of Week#

Day of Week

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

//这道题是有公式的,但是建议别用公式,公式有点不太好记
//采用nextDay思想,这个思想比较通用
#include "stdio.h"
#include "map"
#include "string"
using namespace std;
map<int,string> weekMap = {{1,"Monday"},{2,"Tuesday"},{3,"Wednesday"},{4,"Thursday"},
                           {5,"Friday"},{6,"Saturday"},{7,"Sunday"}};
map<string,int> monthMap = {{"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 monthDay[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
bool leapJudge(int year){
    if(year%4 == 0 && year%100 != 0 || year%400 == 0)
        return true;//true为闰年
    else
        return false;
}

bool bigOrSmall(int day1,int month1,int year1,int day2,int month2,int year2){
    if(year1 > year2)
        return true;//true为1在2日期后
    else if(year1 == year2 && month1 > month2)
        return true;
    else if (year1 == year2 && month1 == month2 && day1 > day2)
        return true;
    else
        return false;//1在2日期前
}

void weekJudge1(int dayAim,int monthAim,int yearAim){
    int day = 9,month = 10,year = 2001;
    int week = 2;bool flag = false;//false表明不是闰年
    while (true){
        if (day == dayAim && month == monthAim && year == yearAim){
            printf("%s\n",weekMap[week].c_str());
            return;
        }
        if (leapJudge(year))
            flag = true;
        else
            flag = false;
        ++week;
        if (week == 8)
            week = 1;
        ++day;
        if(day > monthDay[month] && month != 2){
            day = 1;
            ++month;
        }else if(day > monthDay[month] && month == 2 && flag == false){
            day = 1;
            ++month;
        }else if(day > monthDay[month] && month == 2 && flag == true){
            if (day == 30){
                day = 1;
                ++month;
            }
        }
        if(month > 12){
            month = 1;
            ++year;
        }
    }
}

void weekJudge2(int dayAim,int monthAim,int yearAim){
    int day = 9,month = 10,year = 2001;
    int week = 2;bool flag = false;//false表明不是闰年
    while (true){
        if (day == dayAim && month == monthAim && year == yearAim){
            printf("%s\n",weekMap[week].c_str());
            return;
        }
        if (leapJudge(year))
            flag = true;
        else
            flag = false;
        --week;
        if (week == 0)
            week = 7;
        --day;
        if(day = 0 && month != 3){
            day = monthDay[month-1];
            --month;
        }else if(day = 0 && month == 3 && flag == false){
            day = monthDay[2];
            --month;
        }else if(day = 0 && month == 3 && flag == true){
            day = 29;
            month = 2;
        }
        if(month = 0){
            month = 12;
            --year;
        }
    }
}

int main(){
    int day,year,month;
    char buf[12];
    while (scanf("%d %s %d",&day,buf,&year)!=EOF){
        string str = buf;
        month = monthMap[str];
        if (day == 9 && month == 10 && year == 2001){
            printf("Tuesday\n");
            continue;
        } else{
            if(day,month,year,9,10,2001)
                weekJudge1(day,month,year);
            else
                weekJudge2(day,month,year);
        }
    }
}

全部评论

相关推荐

我是小红是我:学校换成中南
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务