题目描述
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.
输入
输出
样例输入
11 15 1997
1 1 2000
7 4 1998
2 11 1732
9 2 1752
9 14 1752
4 33 1997
0 0 0
样例输出
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.
#include<iostream> using namespace std; int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int judgeyear(int year){ if (year<=1752) { if (year%4==0) return 1; else return 0; } else { if ((year%4==0&&year%100!=0)||year%400==0) return 1; else return 0; } } int judge(int d,int m,int y){ if (m>12||(m!=2&&d>month[m])||(m==2&&judgeyear(y)==1&&d>29)||(m==2&&judgeyear(y)==0&&d>28)||(y==1752&&m==9&&d<14&&d>2)) return 1; } void printmon(int m){ switch(m){ 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; } } void printday(int n){ switch(n){ case 1:cout<<"Monday";break; case 2:cout<<"Tuesday";break; case 3:cout<<"Wednesday";break; case 4:cout<<"Thursday";break; case 5:cout<<"Friday";break; case 6:cout<<"Saturday";break; case 0:cout<<"Sunday";break; } } int p(int d,int m,int y){ int q; int d2=-1; for (int i=1;i<y;i++){ if (judgeyear(i)==1) d2+=366; if (judgeyear(i)==0) d2+=365; } for (int i=0;i<m;i++) d2+=month[i]; if (judgeyear(y)==1&&m>2){ d2++; d2=d2+d-1; } else d2=d2+d-1; if (y>1752||(y==1752&&m>9)||(y==1752&&m==9&&d>=14)) d2-=11; q=d2%7; return q; } int main(){ int d,m,y; while (cin>>m>>d>>y&&d!=0&&m!=0&&y!=0){ if (judge(d,m,y)) cout<<m<<"/"<<d<<"/"<<y<<" is an invalid date."<<endl; else{ printmon(m); cout<<" "<<d<<", "<<y<<" is a "; printday(p(d,m,y)); cout<<endl; } } }