【发布时间】: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++