zoj 1256 What Day Is It?

来源:互联网 发布:php mysql select db 编辑:程序博客网 时间:2024/05/21 20:26
What Day Is It?
点击打开题目链接

Time Limit: 2 Seconds      Memory Limit: 65536 KB

The calendar now in use evolved from the Romans. Julius Caesar codified a calendar system that came to be known as the Julian calendar. In this system, all months have 31 days, except for April, June, September, and November, which have 30 days, and February, which has 28 days in non-leap years, and 29 days in leap years. Also, in this system, leap years happened every four years. That is because the astronomers of ancient Rome computed the year to be 365.25 days long, so that after every four years, one needed to add an extra day to keep the calendar on track with the seasons. To do this, they added an extra day (February 29) to every year that was a multiple of four.

Julian Rule:

Every year that is a multiple of 4 is a leap year, i.e. has an extra day (February 29).

In 1582, Pope Gregory's astronomers noticed that the year was not 365.25 days long, but closer to 365.2425. Therefore, the leap year rule would be revised to the following:

Gregorian Rule:

Every year that is a multiple of 4 is a leap year, unless it is a multiple of 100 that is not a multiple of 400.

To compensate for how the seasons had shifted against the calendar up until that time, the calendar was actually shifted 10 days: the day following October 4, 1582 was declared to be October 15.

England and its empire (including the United States) didn't switch to the Gregorian calendar system until 1752, when the day following September 2 was declared to be September 14. (The delay was caused by the poor relationship between Henry VIII and the Pope.)

Write a program that converts dates in the United States using a calendar of the time and outputs weekdays.


Input

The input will be a series of positive integers greater than zero, three integers per line, which represent dates, one date per line. The format for a date is ``month day year" where month is a number between 1 (which indicates January) and 12 (which indicates December), day is a number between 1 and 31, and year is positive number.


Output

The output will be the input date and name of the weekday on which the given date falls in the format shown in the sample. An invalid date or nonexistent date for the calendar used in the United States at the time should generate an error message indicating a invalid date. The input will end with three zeroes.


Sample Input

11 15 1997
1 1 2000
7 4 1998
2 11 1732
9 2 1752
9 14 1752
4 33 1997
0 0 0


Sample Output

November 15, 1997 is a Saturday
January 1, 2000 is a Saturday
July 4, 1998 is a Saturday
February 11, 1732 is a Friday
September 2, 1752 is a Wednesday
September 14, 1752 is a Thursday
4/33/1997 is an invalid date.



题目要求给出日期,求这天是星期几。涉及到历法的变更:

历法:

教皇格利戈八世于1582年2月24日以教皇训令颁布,将1582年10月5日至14抹掉,于是这10天就消失了,一去不复返。1582年10月4日过完了,第二天已经是10月15日了,于是历法又回到与太阳年同步。

1752年9月,大英帝国极其所属美洲殖民地的恺撒历法被格里高利教皇历法所取代。由于恺撒历法比格里高利历法迟11天,因此9月2日当天改历法后,次日须为9月14日。现行的公历是格利戈里历法,这个历法的是1582年教皇格利戈里根据恺撒大帝引进的算法改进的。它采用的是闰年制也就是现行的制度,不过有一个需要注意的地方就是,这个历法并不是连续的,中间缺少了11天。1752年9月2日之后的那一天并不是1752年9月3日,而是1752年9月14日。也就是说,从1752年9月3日到1752年9月13日的11天并不存在。抹掉这11天是由英国议会做出的决定。所以要计算某年每个月的天数的,除了要考虑是否是闰年以外,还要考虑1752年的9月。


所以此题要考虑全面才不会错;

当出现不应该有的日期时输出, 月/日 /年 /  is an invalid date.

求解时用到了前人总结的公式:

1752年九月二日前的公式:w= (d+ 2*m + 3*(m+1)/5 + Y+ Y/4 +6) % 7;(Y是年份,如果month是1或2时,年份减一)

1752年9月2日之后的公式:w=int(floor(c/4.0)-2*c+y+floor(y/4.0)+floor(26*(m+1)/10.0)+d-1)%7;

其中:w为所求日期的星期数,如果求得的数大于(小于)7,就减去(加上)7的倍数,直到余数小于7为止。c是指公元年份的前两位数字,y是后两位,m是月数,d是日数[]表示对括号内数字取整。特别注意:如果月份是1、2月,则月数为13、14,年份减一,也就是月的范围是3-14。

代码:

#include <iostream>#include<stdio.h>#include<math.h>using namespace std;bool leap(int year)//判断是否是闰年{    if(year%400==0||(year%4==0&&year%100!=0))        return true;    return false;}int week(int year,int month,int day)//根据公式计算星期几{    int c=year/100,y=year%100,d=day,w,m,Y=year;    if(month==1||month==2)    {        m=month+12;        y=(year-1)%100;        c=(year-1)/100;        Y=year-1;    }    else        m=month;    if ((Y< 1752)||((Y== 1752)&&(m< 9))||((Y== 1752)&&(m== 9)&&(day< 3)))/*判断是否在1752年9月3日前*/        w= (d+ 2*m + 3*(m+1)/5 + Y+ Y/4 +6) % 7;/*1752年9月3日前的公式*/    else/*1752年9月3日后的公式*/    w=int(floor(c/4.0)-2*c+y+floor(y/4.0)+floor(26*(m+1)/10.0)+d-1)%7;    if(w<0)        w=(w%7+7)%7;    else        w%=7;    return w;}int main(){    int month,day,year;    int monthday[]= {31,28,31,30,31,30,31,31,30,31,30,31};    while(cin>>month>>day>>year&&(month||day||year))    {        if(leap(year))        {            if((day>monthday[month-1]&&month!=2)||(month==2&&day>(monthday[1]+1))||month>12||month<=0||day<=0||year<=0||(year==1752&&month==9&&day>2&&day<14))            //判断是否为合法日期                 printf("%d/%d/%d is an invalid date.\n",month,day,year);            else            {                switch(month)                {                case 1:                    cout<<"January ";                    break;                case 2:                    cout<<"February ";                    break;                case 3:                    cout<<"March ";                    break;                case 4:                    cout<<"April ";                    break;                case 5:                    cout<<"May ";                    break;                case 6:                    cout<<"June ";                    break;                case 7:                    cout<<"July ";                    break;                case 8:                    cout<<"August ";                    break;                case 9:                    cout<<"September ";                    break;                case 10:                    cout<<"October ";                    break;                case 11:                    cout<<"November ";                    break;                case 12:                    cout<<"December ";                    break;                }                printf("%d, %d is a ",day,year);                switch(week(year,month,day))                {                case 0:                    cout<<"Sunday"<<endl;                    break;                case 1:                    cout<<"Monday"<<endl;                    break;                case 2:                    cout<<"Tuesday"<<endl;                    break;                case 3:                    cout<<"Wednesday"<<endl;                    break;                case 4:                    cout<<"Thursday"<<endl;                    break;                case 5:                    cout<<"Friday"<<endl;                    break;                case 6:                    cout<<"Saturday"<<endl;                    break;                }            }        }        else        {            if(day>monthday[month-1]||month>12||month<=0||day<=0||year<=0||(year==1752&&month==9&&day>2&&day<14))                printf("%d/%d/%d is an invalid date.\n",month,day,year);            else            {                switch(month)                {                case 1:                    cout<<"January ";                    break;                case 2:                    cout<<"February ";                    break;                case 3:                    cout<<"March ";                    break;                case 4:                    cout<<"April ";                    break;                case 5:                    cout<<"May ";                    break;                case 6:                    cout<<"June ";                    break;                case 7:                    cout<<"July ";                    break;                case 8:                    cout<<"August ";                    break;                case 9:                    cout<<"September ";                    break;                case 10:                    cout<<"October ";                    break;                case 11:                    cout<<"November ";                    break;                case 12:                    cout<<"December ";                    break;                }                printf("%d, %d is a ",day,year);                switch(week(year,month,day))                {                case 0:                    cout<<"Sunday"<<endl;                    break;                case 1:                    cout<<"Monday"<<endl;                    break;                case 2:                    cout<<"Tuesday"<<endl;                    break;                case 3:                    cout<<"Wednesday"<<endl;                    break;                case 4:                    cout<<"Thursday"<<endl;                    break;                case 5:                    cout<<"Friday"<<endl;                    break;                case 6:                    cout<<"Saturday"<<endl;                    break;                }            }        }    }    return 0;}
仔细很重要啊,( ⊙ o ⊙ )


0 0