USACO。十五号星期五。我的代码出了什么问题?

时间:2014-01-20 20:14:07

标签: c++ logic task days

我为USACO任务制定了一个程序,现在我无法弄清楚为什么我会得到错误的答案,我一直在想为什么很长一段时间仍然没有弄明白。可能有什么不对?

任务是计算第13次星期六,星期日,星期一,星期二,......,星期五的次数。从测试时间到1900年,最后一年在txt文件中给出。

而不是这个答案:36 33 34 33 35 35 34

我得到:34 33 33 33 36 36 35

非常感谢你的帮助,我真的非常感谢你的时间和帮助:)

#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 个答案:

答案 0 :(得分:0)

您的月份比较不正确。

2月是第2个月,而不是第5个月 作为第二个月,它没有30天(至少自从我活着以来)。

您可能想要交换这些常量。