Usaco. Friday the thirteen. What is wrong with my

2019-09-21 15:23发布

I've made a program for the USACO task and now I just can't figure out why I'm getting the bad answer, I've been thinking why for a long time and still didn't figure it out. What could be wrong?

The task is to count the number of times the 13th falls on Saturday, Sunday, Monday, Tuesday, ..., Friday. The year from when to test is 1900 and the last year is given in the txt file.

Instead of this answer: 36 33 34 33 35 35 34

I'm getting: 34 33 33 33 36 36 35

Thank you very very very much for you help, I really apreciate your time and your help :)

#include<iostream>
#include<fstream>

using namespace std;

int weekDayCount(int days, int weekDays[], int & currentWeekDay)
{

for(int day = 1; day <= days; day++)
{      
    if(day == 13)
    {
        weekDays[currentWeekDay]++;
    }

    currentWeekDay++;

    if(currentWeekDay == 7) currentWeekDay = 0;
}
}

int main()
{
int currentWeekDay = 0;
int years;

ifstream in("friday.in");

in >> years;

in.close();

int weekDays[7] = {0};

for(int y = 1900; y <= (1900 + years) - 1; y++) // Years
{
    cout << y << endl;

    for(int m = 1; m <= 12; m++) // Months
    {
        if(m == 11 || m == 2 || m == 10 || m == 8) // 30 days
        {
            weekDayCount(30, weekDays, currentWeekDay);
        }
        else if(m == 5) // February
        {
            if(y % 100 != 0 && y % 4 == 0) // If a leap year - 29 days
            {
                weekDayCount(29, weekDays, currentWeekDay);
            }
            else if(y % 100 == 0 && y % 400 == 0) // If a century leap year
            {
                cout << "Leap century: " << y << endl;
                weekDayCount(29, weekDays, currentWeekDay);
            }
            else // 28 days
            {
                weekDayCount(28, weekDays, currentWeekDay);
            }
        }
        else // Else 31 days
        {
            weekDayCount(31, weekDays, currentWeekDay);
        }
    }
}

cout << "Result" << endl;

 cout << weekDays[5] << " " << weekDays[6] << " " << weekDays[0] << " " << weekDays[1] << " " << weekDays[2] << " " << weekDays[3] << " " << weekDays[4] << endl;
}

1条回答
对你真心纯属浪费
2楼-- · 2019-09-21 15:59

Your month comparisons are not correct.

February is the 2nd month, not the 5th.
As the second month, it doesn't have 30 days (at least since I've been alive).

You may want to swap those constants.

查看更多
登录 后发表回答