其他分享
首页 > 其他分享> > Day of Week 【日期】【模拟】

Day of Week 【日期】【模拟】

作者:互联网

Day of Week题意

思路

代码

#include<bits/stdc++.h>
using namespace std;
int date[13]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
string strm[13]={"","January", "February", "March", "April", "May", "June",
              "July", "August", "September", "October", "November", "December"};
string strw[7] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
int y, m, d;
int yearcnt = 0;
int my = 2021, mm = 12, md = 31, mw = 4;
bool isLeap(int year){
    if((!(year%4)&&(year%100))||!(year%400)){
        date[2]=29;
        yearcnt = 366;
        return true;
    }
    else{
        date[2]=28;
        yearcnt = 365;
        return false;
    }
}
int main(){
    string str;
    while(cin>>d>>str>>y){
        for(int i=1;i<13;i++)
        {
            if(str.size()==strm[i].size() && str==strm[i]){
                m = i;
                break;
            }
        }
        int iy, im, id, iw=mw;
        for(iy = my; iy!=y; ){
            // 注意这里未来和过去的区别!
            if(y>iy){
                // 要加上未来一年的日子,而不是现在这一年
                isLeap(++iy);
                iw = (iw+(yearcnt%7))%7;
            }
            else{
                // 要减去这一年的日子
                isLeap(iy);
                iw = (iw-(yearcnt%7)+7)%7;
                iy--;
            }
        }
        isLeap(iy);
        for(im = mm; im!=m; ){
            if(m>im){
                iw = (iw + date[im]%7)%7;
                im++;
            }
            else{
                iw = (iw - date[im]%7 + 7) % 7;
                im--;
            }
        }
        if(date[im]<d){
            iw = (iw + (d - date[im])%7)%7;
        }
        else{
            iw = (iw - (date[im] - d)%7 + 7)%7;
        }
        cout<<strw[iw]<<endl;
    }
    return 0;
}

标签:Week,int,31,iw,isLeap,im,date,Day,模拟
来源: https://www.cnblogs.com/xuwanwei/p/14648849.html