USACO.星期五十三.我的代码有什么问题
Usaco. Friday the thirteen. What is wrong with my code?
我已经为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;
}
您的月比较不正确。
2月是第二个月,而不是第五个月。
作为第二个月,它没有30天(至少以来我还活着)。
您可能想交换那些常数。
相关文章:
- 警告处理为错误这里有什么问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 当我尝试添加 2 个大字符串时,我无法弄清楚出了什么问题
- 违反const正确性:我应该现实地期待什么问题
- 这个带有模板<类 Vector 的C++代码片段有什么问题>
- 我的逻辑反转字符串中的元音有什么问题?
- 需要以下代码的帮助,下面的代码有什么问题
- 常量公共成员有什么问题?
- 以下代码中的函数模板有什么问题?
- 这个返回元素位置的基于循环的函数有什么问题?
- creat_list2功能有什么问题?
- 格式说明符C++有什么问题
- 任何人都可以告诉我我的 C++ 代码出了什么问题?
- 从 argv[1] 转换为字符 * 字符串后有什么问题?
- 我的堆栈和库存清单程序的结构有什么问题?
- 此工厂功能有什么问题?
- 以下 C++ 代码有什么问题?
- 数组为此合并排序函数提供了正确的输出,但向量给出了不正确的输出.出了什么问题?
- reinterpret_cast,只读访问,简单的可复制类型,会出什么问题?
- 它解决了什么问题,对于非真空初始化,生命周期在初始化之前就开始了