九度OJ 1043 蔡勒公式

来源:互联网 发布:淘宝 国际米兰球衣 编辑:程序博客网 时间:2024/04/30 10:48

如何计算某一天是星期几?
—— 蔡勒(Zeller)公式 
历史上的某一天是星期几?未来的某一天是星期几?关于这个问题,有很多计算公式(两个通用计算公式和一些分段计算公式),其中最著名的是蔡勒(Zeller)公式。即w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1

公式中的符号含义如下,w:星期;c:世纪-1;y:年(两位数);m:月(m大于等于3,小于等于14,即在蔡勒公式中,某年的1、2月要看作上一年的13、14月来计算,比如2003年1月1日要看作2002年的13月1日来计算);d:日;[ ]代表取整,即只要整数部分。(C是世纪数减一,y是年份后两位,M是月份,d是日数。1月和2月要按上一年的13月和 14月来算,这时C和y均按上一年取值。)

算出来的W除以7,余数是几就是星期几。如果余数是0,则为星期日。

以2049年10月1日(100周年国庆)为例,用蔡勒(Zeller)公式进行计算,过程如下: 
蔡勒(Zeller)公式:w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1 
=49+[49/4]+[20/4]-2×20+[26× (10+1)/10]+1-1 
=49+[12.25]+5-40+[28.6] 
=49+12+5-40+28 
=54 (除以7余5) 
即2049年10月1日(100周年国庆)是星期5。


这个是最简单的算法

蔡勒(Zeller)公式:w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1 


不过,以上公式只适合于1582年10月15日之后的情形(当时的罗马教皇将恺撒大帝制订的儒略历修改成格里历,即今天使用的公历


输入:

There is one single line contains the day number d, month name M and year number y(1000≤y≤3000). The month name is the corresponding English name starting from the capital letter.

输出:

Output a single line with the English name of the day of week corresponding to the date, starting from the capital letter. All other letters must be in lower case.

样例输入:
9 October 200114 October 2001
样例输出:
TuesdaySunday
#include<stdio.h>char* weeks[]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};int dayofweek(int y,int m,int d){    if(m<3)    {        y-=1;        m+=12;    }    int c=y/100;    y=y%100;    int w=y+y/4+c/4-2*c+26*(m+1)/10+d-1;//这就是蔡勒公式啦~~~~~~~~    while(w<0)        w+=7;    printf("%s\n",weeks[w%7]);}int stringToMonth(char *month){    if(strcmp(month, "January") == 0)    {        return 1;    }else if(strcmp(month, "February") == 0)    {        return 2;    }else if(strcmp(month, "March") == 0)    {        return 3;    }else if(strcmp(month, "April") == 0)    {        return 4;    }else if(strcmp(month, "May") == 0)    {        return 5;    }else if(strcmp(month, "June") == 0)    {        return 6;    }else if(strcmp(month, "July") == 0)    {        return 7;    }else if(strcmp(month, "August") == 0)    {        return 8;    }else if(strcmp(month, "September") == 0)    {        return 9;    }else if(strcmp(month, "October") == 0)    {        return 10;    }else if(strcmp(month, "November") == 0)    {        return 11;    }else if(strcmp(month, "December") == 0)    {        return 12;    }}int main(int argc, char *argv[]){    char m[20];    int y;    int d;    //freopen("in.txt","r",stdin);    while(scanf("%d %s %d",&d,m,&y)!=EOF)    {        dayofweek(y,stringToMonth(m),d);    }    return 0;}

蔡勒公式的推理过程:蔡勒公式

0 0
原创粉丝点击