欧拉19输出

时间:2017-10-20 04:06:54

标签: c

这是针对Project Euler 19.我几乎想出了代码,但出于某种原因我的输出为+1。

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

1 个答案:

答案 0 :(得分:0)

#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 = 366; /* 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;
                }
                else{
                    daysinmonths[1] = 28;
                }
                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);
   }