Bovine Birthday

Bessie asked her friend what day of the week she was born on. She knew that she was born on 2003 May 25, but didn't know what day it was. Write a program to help. Note that no cow was born earlier than the year 1800.

Facts to know:

* January 1, 1900 was on a Monday.

* Lengths of months:
    Jan 31          May 31      Sep 30
    Feb 28 or 29    Jun 30      Oct 31
    Mar 31          Jul 31      Nov 30
    Apr 30          Aug 31      Dec 31

* Every year evenly divisible by 4 is a leap year (1992 = 4*498 so 1992 will be a leap year, but the year 1990 is not a leap year).

* The rule above does not hold for century years. Century years divisible by 400 are leap years, all other are not. Thus, the century years 1700, 1800, 1900 and 2100 are not leap years, but 2000 is a leap year.
Input
* Line 1: Three space-separated integers that represent respectively the year, month (range 1..12), and day of a date.
Output
* Line 1: A single word that is the day of the week of the specified date (from the lower-case list: monday, tuesday, wednesday, thursday, friday, saturday, sunday).
Sample Input
2003 5 25
Sample Output
sunday
Hint
INPUT DETAILS:
May 25, 2003

OUTPUT DETAILS:
      May 2003
Su Mo Tu We Th Fr Sa
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
题意:求一个日期是星期几,不太清楚dp的做法,网上我也没找到,网上是菜勒公式做的,感觉很简单。我的方法应该比较好想,就是求1800年到指定日期有几天,再对7求余,余数从0-6分别对于星期二至星期日。
#include <stdio.h>

int main()
{
    int y,m,d;
    int i,j,k;
    int ret;
    int sum = 0;
    int countn = 0;
    while(scanf("%d%d%d",&y,&m,&d) != EOF)
    {
        sum = 0;
        countn = 0;
            for(i = 1800;i < y;i++)
            {
                if((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)
                {
                    countn++;
                }
            }
            sum = sum + (y - 1800 - countn) * 365 + countn * 366;
        switch(m)
        {
        case 12:
            {
                sum += 30;
            }
        case 11:
            {
                sum += 31;
            }
        case 10:
            {
                sum += 30;
            }
        case 9:
            {
                sum += 31;
            }
        case 8:
            {
                sum += 31;
            }
        case 7:
            {
                sum += 30;
            }
        case 6:
            {
                sum += 31;
            }
        case 5:
            {
                sum += 30;
            }
        case 4:
            {
                sum += 31;
            }
        case 3:
            {
                if((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
                {
                    sum += 29;
                }
                else
                {
                    sum += 28;
                }
            }
        case 2:
            {
                sum += 31;
            }
        case 1:
            {
                sum += d;
            }
        }
            ret = sum % 7;
        switch(ret)
        {
        case 5:
            {
                printf("sunday\n");
                break;
            }
        case 6:
            {
                printf("monday\n");
                break;
            }
        case 0:
            {
                printf("tuesday\n");
                break;
            }
        case 1:
            {
                printf("wednesday\n");
                break;
            }
        case 2:
            {
                printf("thursday\n");
                break;
            }
        case 3:
            {
                printf("friday\n");
                break;
            }
        case 4:
            {
                printf("saturday\n");
                break;
            }
        }
    }

    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值