九度OJ 题目1043:Day of Week

时间:2021-01-19 20:16:01

本算法中引用的是由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;}