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>
-
-
int dayofmouth(int year, int mouth)
-
{
-
int ret;
-
-
switch(mouth) {
-
case 1:
-
case 3:
-
case 5:
-
case 7:
-
case 8:
-
case 10:
-
case 12:
-
ret = 31;
-
break;
-
case 4:
-
case 6:
-
case 9:
-
case 11:
-
ret = 30;
-
break;
-
case 2:
-
ret = (!(year%4) && (year%100) || !(year%400))?29:28;
-
break;
-
default:
-
ret = -1;
-
}
-
-
return ret;
-
}
-
-
int main(int argc, const char *argv[])
-
{
-
int remain;
-
int i, j;
-
int result = 0;
-
-
remain = 365 % 7; /* 1900 */
-
-
for (i=1901; i<2001; i++) {
-
for (j=1; j<=12; j++) {
-
remain = (remain + dayofmouth(i, j)) % 7;
-
if(remain == 6)
-
result++;
-
}
-
}
-
-
printf("result: %d\n", result);
-
-
return 0;
-
}
阅读(1414) | 评论(0) | 转发(0) |