(Problem 19)Counting Sundays

时间:2022-07-20 08:05:50

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

  • 1 Jan 1900 was a Monday.
  • Thirty days has September,
    April, June and November.
    All the rest have thirty-one,
    Saving February alone,
    Which has twenty-eight, rain or shine.
    And on leap years, twenty-nine.
  • A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.

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

#include <stdio.h>
#include <stdbool.h> const int a[][] = {{,,,,,,,,,,,},
{,,,,,,,,,,,}}; bool leapYear(int n) //判断闰年
{
return (((n % ==) && (n % !=)) || (n % == ));
} bool issunday(int n) //判断某天是否是星期天
{
return (n % == ? true : false);
} void solve(void)
{
int num, i, j, count;
count = ; i = ;
num = ;
while(i < ) { int t = (leapYear(i) ? : ); //判断闰年
for(j = ; j < ; j++) {
num += a[t][j];
if(issunday(num)) count++;
}
i++;
}
printf("%d\n",count);
} int main(void)
{
solve();
return ;
}
Answer:
171