九度OJ 题目1043:Day of Week

标签: 算法
1883人阅读 评论(1) 收藏 举报
分类:

本算法中引用的是由Tomohiko Sakamoto 提供的简洁函数代码

代码如下:

int dayofweek(int y, int m, int d) /* 0 = Sunday */
{
       static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
       y -= m < 3;
       return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}

AC代码如下:

#include <stdio.h>

int dayofweek(int y, int m, int d) /* 0 = Sunday */
{
       static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
       y -= m < 3;
       return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}

const char day[][20] = {"Sunday","Monday","Tuesday","Wednesday",
                    "Thursday","Friday","Saturday"};

const char month[][20] = {"January", "February", "March", "April",
                    "May", "June", "July", "August", "September",
                     "October", "November", "December"};

int main()
{
    char m[20];
    int d,y,mon;
    int i;
    while(scanf("%d %s %d",&d,m,&y) != EOF)
    {
        for(i = 0;i < 12;i++)
        {
            if(strcmp(m,month[i]) == 0)
            {
                mon = i;
                break;
            }
        }
        //printf("%d %d %d\n",y,mon + 1,d);
        printf("%s\n",day[dayofweek(y,mon + 1,d)]);
    }
    return 0;
}


2
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2375813次
    • 积分:26947
    • 等级:
    • 排名:第212名
    • 原创:383篇
    • 转载:106篇
    • 译文:3篇
    • 评论:652条
    博客专栏
    最新评论