计算某一天是星期几的算法

来源:互联网 发布:skype官网for mac 编辑:程序博客网 时间:2024/06/07 20:34

如何计算某一天是星期几?
—— 蔡勒(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日之后的情形(当时的罗马教皇将恺撒大帝制订的儒略历修改成格里历,即今天使用的公历)。


http://codeup.cn/problem.php?cid=100000578&pid=1
http://ac.jobdu.com/problem.php?pid=1043
题目1043:Day of Week

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:5834

解决:2090

题目描述:

We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400.
For example, years 2004, 2180 and 2400 are leap. Years 2004, 2181 and 2300 are not leap.
Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.

输入:

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
提示:

Month and Week name in Input/Output:
January, February, March, April, May, June, July, August, September, October, November, December
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday

来源:
2008年上海交通大学计算机研究生机试真题
答疑:
解题遇到问题?分享解题心得?讨论本题请访问:http://t.jobdu.com/thread-7767-1-1.html
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<map>using namespace std;map<string,int>month;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; //引用了Tomohiko Sakamoto 提供的简洁代码}int main(){int y,m,d;string mon;    char weekday[7][10]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};        month["January"]=1;month["February"]=2;month["March"]=3;month["April"]=4;month["May"]=5;month["June"]=6;month["July"]=7;month["August"]=8;month["September"]=9;month["October"]=10;month["November"]=11;month["December"]=12;    while(cin>>d>>mon>>y)    {    m=month[mon];    int week = dayofweek(y,m,d);    cout<<weekday[week]<<endl;}       return 0;}


受教了,自己真的没辙,才发现这个的。还在努力切水题,争取18号前切完所有codeup和PAT里的水题。
之后要开始STL和数据结构的链表 队 栈,之后两周分别给树 和图。加油吧!


0 0
原创粉丝点击