【问题标题】:What is wrong with my code? Usaco: Friday the Thirteenth我的代码有什么问题?乌萨科:十三号星期五
【发布时间】:2015-03-11 12:38:32
【问题描述】:

我被自己的代码搞砸了。这个问题来自 train.usaco.org:十三号星期五。

目标如下:也就是说,每月 13 日在星期五的频率是否低于一周中的任何其他日子?要回答这个问题,请编写一个程序来计算在给定的 N 年期间每个月的 13 日在星期日、星期一、星期二、星期三、星期四、星期五和星期六出现的频率。

这是我的代码:

/*
ID: erdemtu2
PROG: friday
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;

int horwuul(int month, int year){
    if(month == 2)
        return ((year % 4) || (!(year % 100) && ((year+300) % 400))) ? 28 : 29;
    switch(month){
        case 3:
        case 5:
        case 9:
        case 11:
            return 30;
            break;
        default:
            return 31;
            break;
    }
}

int main() {
    //freopen("friday.in", "r", stdin);
    //freopen("friday.out", "w", stdout);

    int a, i, j;
    cin >> a;
    if(a < 0) a = 0;
    if(a > 400) a = 400;

    int ans[7]={0};
    //was int ans[7];

    int q=0, month=0;
    for(i = 0; i < a; i++)
        for(j = 0; j < 12; j++){
            ans[((q + 6) % 7)]++;
            q = (q + horwuul(j, i)) % 7;
        }

    for(i = 0; i < 7; i++){
        cout << ans[(i + 6) % 7];
        cout << " ";
    }
    return 0;
}

预期

  • 输入:20
  • 输出:36 33 34 33 35 35 34

我的 曾是: - 输出:36 4233426 2293443 2293497 2293733 1996393719 -52161590 现在: -输出:38 34 35 33 33 34 33

我弄错了什么?任何帮助都会很棒,感谢您的回复。

【问题讨论】:

  • 这个问题似乎假设 N 年的所有时期都具有相同的“13th”分布。
  • 无论如何,初始化你的ans数组(例如int ans[7] = {0})。
  • 感谢您的回复。感谢您让我知道如何将循环与数组一起使用。

标签: c++


【解决方案1】:

每年从一月开始。您可以决定一月是 0 还是 1,但您应该保持一致。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-25
    • 2011-07-28
    相关资源
    最近更新 更多