Chinaunix首页 | 论坛 | 博客
  • 博客访问: 244894
  • 博文数量: 47
  • 博客积分: 1229
  • 博客等级: 中尉
  • 技术积分: 568
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-20 10:06
文章分类

全部博文(47)

文章存档

2014年(1)

2013年(7)

2012年(1)

2011年(38)

分类: C/C++

2011-03-07 23:17:52

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)?
--------------------
  1. #include <stdio.h>

  2. int dayofmouth(int year, int mouth)
  3. {
  4.     int ret;

  5.     switch(mouth) {
  6.         case 1:
  7.         case 3:
  8.         case 5:
  9.         case 7:
  10.         case 8:
  11.         case 10:
  12.         case 12:
  13.             ret = 31;
  14.             break;
  15.         case 4:
  16.         case 6:
  17.         case 9:
  18.         case 11:
  19.             ret = 30;
  20.             break;
  21.         case 2:
  22.             ret = (!(year%4) && (year%100) || !(year%400))?29:28;
  23.             break;
  24.         default:
  25.             ret = -1;
  26.     }

  27.     return ret;
  28. }

  29. int main(int argc, const char *argv[])
  30. {
  31.     int remain;
  32.     int i, j;
  33.     int result = 0;

  34.     remain = 365 % 7;    /* 1900 */

  35.     for (i=1901; i<2001; i++) {
  36.         for (j=1; j<=12; j++) {
  37.             remain = (remain + dayofmouth(i, j)) % 7;
  38.             if(remain == 6)
  39.                 result++;
  40.         }
  41.     }

  42.     printf("result: %d\n", result);

  43.     return 0;
  44. }



阅读(1414) | 评论(0) | 转发(0) |
0

上一篇:euler11

下一篇:euler21

给主人留下些什么吧!~~