Usaco。星期五十三。我的代码有什么问题? [英] Usaco. Friday the thirteen. What is wrong with my code?

查看:160
本文介绍了Usaco。星期五十三。我的代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为USACO任务制定了一个程序,现在我不知道为什么我得到了错误的答案,我一直在想为什么很长时间,仍然没有想出来。什么可能是错误的?

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?

任务是计算第13个星期六,星期日,星期一,星期二,...,星期五的次数。

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.

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

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

我得到:34 33 33 33 36 36 35

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

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

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;
}


推荐答案

2月是第2个月,而不是第5个月。

第二个月没有30天已存活)。

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.

这篇关于Usaco。星期五十三。我的代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆