What Day Is It?

时间限制: 2 Sec   内存限制: 64 MB

题目描述

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.

输入

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.

输出

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.

样例输入

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.
对于这个题来说,算法方面并不算难,但是这个题需要认真仔细的分析,首先,这个题关于闰年的判断是分为两个时期的判断,从1752年之前与1752年之后,闰年的判断是两种情况。然后判断这个日期是否合法,(如果月份超过12,或者是每月的天数超过最大的要求,或者是根据题目要求1752年没有9.2-9.14就好了。
这个题最重要的就是认真,因为有好多地方有一些细微的地方需要注意,有bug的话多调试几次就好了。


#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;
		}
	}
}                                              

猜你喜欢

转载自blog.csdn.net/clintseven/article/details/80670043
今日推荐