首页 > ACM题库 > HDU-杭电 > HDU 1308 What Day Is It?-模拟题-[解题报告] C++

HDU 1308 What Day Is It?-模拟题-[解题报告] C++

What Day Is It?

问题描述 :

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.


这个题目的主要问题是干扰信息太多了。1. 按照英美的日历的标准就是1752年之后算闰年的方法才改变的。2. 要知道某一天是星期几,必须知道 1.1.1 是星期几


#include <iostream>
#include <string>
using namespace std;
const string MonthName[]={"January", "February", "March", "April", "May", "June", "July",
	"August", "September", "October", "November", "December"};
const string WeekName[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
const int DayNum[]={31,0,31,30,31,30,31,31,30,31,30,31};
bool isLeap(int y)
		return (y%400==0 || ((y%4==0)&&(y%100!=0)));
	else return y%4==0;
int YearDays(int y)
	return (isLeap(y) ? 366 : 365);
int MonthDays(int y,int m)
	if(m != 2) return DayNum[m-1];
	else //m==2
		if(isLeap(y)) return 29;
		else return 28;
bool isValid(int y, int m, int d)
	if(m<1 || m>12) return false;
	if(d<1 || d>MonthDays(y,m)) return false;
	if(y==1752 && m==9 && d>=3 && d<=13) return false;
	return true;
struct Date
	int Y,M,D;
	Date(int y,int m,int d):Y(y),M(m),D(d){}
istream& operator>>(istream& in, Date& date)
	return in;
ostream& operator<<(ostream& out, const Date& date)
	out<<MonthName[date.M-1]<<' '<<date.D<<", "<<date.Y;
	return out;
bool operator<(const Date& lh,const Date& rh)
	if(lh.Y != rh.Y) return (lh.Y < rh.Y);
		if(lh.M != rh.M) return (lh.M < rh.M);
		else return (lh.D < rh.D);
int main()
	Date date;
		if(date.Y==0 && date.M==0 && date.D==0) break;
			cout<<date.M<<'/'<<date.D<<'/'<<date.Y<<" is an invalid date.\n";continue;
		int days=-1,i; //1 1 1到底是星期几?
		for(i=1;i<date.Y;++i) days += YearDays(i);
		for(i=1;i<date.M;++i) days += MonthDays(date.Y, i);
		days += (date.D-1);
		if(Date(1752,9,2) < date) days -= 11;
		cout<<date<<" is a "<<WeekName[days%7]<<endl;
	return 0;


  1. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。

  2. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。