Chinaunix首页 | 论坛 | 博客
  • 博客访问: 216496
  • 博文数量: 68
  • 博客积分: 3120
  • 博客等级: 中校
  • 技术积分: 715
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-08 09:53
文章分类
文章存档

2012年(29)

2011年(3)

2010年(18)

2009年(18)

我的朋友

分类: C/C++

2012-01-25 17:40:18

问题:

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)?

答案:171

#include

int main()
{
    int d[1212]={0};
    int year=1900, mon=1;

    int i=0;

    for(i=0; i<1212; i++)
    {
        year = i / 12 + 1900;
        mon = i % 12 + 1;

        if (i==0){
            d[0]=1;
             continue;
        }

        if ((year%4 != 0) || (year%100 == 0 && year % 400 != 0))
        {
            switch(mon)
            {
                case 1:
                case 2:
                case 4:
                case 6:
                case 8:
                case 9:
                case 11:
                    d[i] = 31+d[i-1];
                    break;
                case 3:
                    d[i] = 28+d[i-1];
                    break;   
                case 5:
                case 7:
                case 10:
                case 12:
                    d[i] = 30+d[i-1];
                    break;
            }
        }
        else
        {
            switch(mon)
            {
                case 1:
                case 2:
                case 4:
                case 6:
                case 8:
                case 9:
                case 11:
                    d[i] = 31+d[i-1];
                    break;
                case 3:
                    d[i] = 29+d[i-1];
                    break;   
                case 5:
                case 7:
                case 10:
                case 12:
                    d[i] = 30+d[i-1];
                    break;
            }
        }
    }

    int count=0;

    for (i=12; i<1212; i++)
        if(d[i]%7 == 0) count++;
        //printf("%5d ", d[i]);

    printf("The number of Sunday is %d\n", count);
    return 0;
}


阅读(720) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~