这是为欧拉19.我几乎想出了代码,但由于某种原因,我的输出是+1。欧拉19输出
#include <stdio.h>
#define SIZE 12
int main(void)
{
int year;
int month;
int daysinmonths[SIZE] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int currentday = 365; /* Account for 1900 */
int sundaycount = 0;
for (year = 1901; year <= 2000; year++) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
daysinmonths[1] = 29;
}
for (month = 0; month < SIZE; month++) {
if (currentday % 7 == 0)
sundaycount++;
currentday += daysinmonths[month];
}
}
printf("%d Sundays as the first of a month from 1901 to 2000 \n", sundaycount);
}
如果你所描述的欧拉19,节省大家搜索这将是很好。另外“我的输出是+1”是什么意思? –
输出应该是171,但我得到172,因此+1。 Euler 19要求我们找出从1901年到2000年这个月的第一个星期几是多少。 – kamisama
第一次遇到闰年时,您将二月份的天数设置为29.您将它重新设置为28? –