简体   繁体   English

获得一个月的第 b 个星期的程序

[英]program to get a month the bth c day of the week

For example, get the date of the third Friday in October from 2000 to 2015. and I know that 1850-1-1 is Tuesday.例如,获取从 2000 年到 2015 年 10 月的第三个星期五的日期。我知道 1850-1-1 是星期二。

I got 80 score on OJ (which is less than the maximum).我在 OJ 上得了 80 分(低于最高分)。 I don't know where the bug is.我不知道错误在哪里。 Please help me find the problem.请帮我找出问题所在。 I have tried for a long time but could not get the problem.我已经尝试了很长时间,但无法解决问题。 Below is my code:下面是我的代码:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int daysFrom[250];
bool year_check(int year);
string int2String(int a);
int main(){
    unsigned int month2days[13];
    month2days[0]=0;
    month2days[1]=31;
    month2days[2]=0;
    month2days[3]=31;
    month2days[4]=30;
    month2days[5]=31;
    month2days[6]=30;
    month2days[7]=31;
    month2days[8]=31;
    month2days[9]=30;
    month2days[10]=31;
    month2days[11]=30;
    month2days[12]=31;
    int a;
    cin>>a;
    int b;
    cin>>b;
    int c;
    cin>>c;
    int y1;
    cin>>y1;
    int y2;
    cin>>y2;
    daysFrom[0]=0;
    for(int i=1;i<250;i++){
        int year=1850+i;
        if(year_check(year-1)){
            daysFrom[i]=daysFrom[i-1]+366;
        }else{
            daysFrom[i]=daysFrom[i-1]+365;
        }
    }
    for(int yearP=y1;yearP<=y2;yearP++){
        unsigned int days=daysFrom[yearP-1850];
        for(int monthP=1;monthP<a;monthP++){
            if(monthP==2){
                if(year_check(yearP)){
                    days+=29;
                }else{
                    days+=28;
                }
            }else{
                days+=month2days[monthP];
            }
        }
        int weekPos=days%7;
        weekPos=(weekPos+2)%7;
        int dis=0;
        if(c<weekPos){
            dis=7-weekPos+c+1+(b-1)*7;
        }else{
            dis=c-weekPos+1+(b-1)*7;
        }
        if(a==2){
            if(year_check(yearP)){
                if(dis>29){
                    cout<<"none"<<endl;
                }else{
                    cout<<yearP<<"/"<<int2String(a)<<"/"<<int2String(dis)<<endl;
                }
            }else{
                if(dis>28){
                    cout<<"none"<<endl;
                }else{
                    cout<<yearP<<"/"<<int2String(a)<<"/"<<int2String(dis)<<endl;
                }
            }
        }else{
            if(dis>month2days[a]){
                cout<<"none"<<endl;
            }else{
                cout<<yearP<<"/"<<int2String(a)<<"/"<<int2String(dis)<<endl;
            }
        }
    }
    return 0;
}
bool year_check(int year){
    if(year%400==0){
        return true;
    }else{
        if(year%4==0&&year%100!=0){
            return true;
        }
    }
    return false;
}
string int2String(int a){
    switch(a){
    case 1:
        return "01";
    case 2:
        return "02";
    case 3:
        return "03";
    case 4:
        return "04";
    case 5:
        return "05";
    case 6:
        return "06";
    case 7:
        return "07";
    case 8:
        return "08";
    case 9:
        return "09";
    default:
        stringstream str;
        str<<a;
        return str.str();
    }
}

I got it.我知道了。 problem is问题是

weekPos=(weekPos+2)%7;

sunday is 0. actually it is 7.星期天是 0。实际上是 7。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM