首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Euler 19输出

Euler 19输出
EN

Stack Overflow用户
提问于 2017-10-20 12:06:54
回答 1查看 50关注 0票数 1

这是为Project Euler 19准备的。我几乎弄明白了代码,但出于某种原因,我的输出是+1。

代码语言:javascript
复制
#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);
   }
EN

回答 1

Stack Overflow用户

发布于 2017-10-20 12:20:15

代码语言:javascript
复制
#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);
   }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46842219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档