简体   繁体   中英

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.

I got 80 score on OJ (which is less than the maximum). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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