(Problem 19)Counting Sundays

时间:2014-07-22 23:17:23   收藏:0   阅读:552

You are given the following information, but you may prefer to do some research for yourself.

How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?

 

bubuko.com,布布扣
#include <stdio.h> 
#include <stdbool.h>

const int a[2][12] = {{31,28,31,30,31,30,31,31,30,31,30,31},
                     {31,29,31,30,31,30,31,31,30,31,30,31}};


bool leapYear(int n)  //判断闰年
{
    return (((n % 4 ==0) && (n % 100 !=0)) || (n % 400 == 0));
}

bool issunday(int n) //判断某天是否是星期天
{
    return (n % 7 == 0 ? true : false);
}

void solve(void)
{
    int num, i, j, count;
    count = 0;

    i = 1901;
    num = 1;
    while(i < 2000) {

        int t = (leapYear(i) ? 1 : 0);   //判断闰年
        for(j = 0; j < 12; j++) {
            num += a[t][j];
            if(issunday(num)) count++;
        }
        i++;
    }
    printf("%d\n",count);
}

int main(void)
{
    solve();
    return 0;
}
bubuko.com,布布扣
Answer:
171

原文:http://www.cnblogs.com/acutus/p/3514075.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!