ZOJ 1256 What Day Is It?(有点坑)

What Day Is It?

Time Limit: 2000 msMemory 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.

思路:

题目大意是说在1752年9月2日前美国采用的是Julius历,每四年闰一次,而在这天之后为了历法的精确性,将9月3号改成了9月14号,之后给出一些年月日的样例,要求输出当天是星期几。
思路就是算出从公元元年的1月1号起到当前的日期一共有多少天,然后对7取余即可。主要是要注意闰年规则的变化!!!

AC代码:

#include
#include

using namespace std;
int days[] = { 0, 31,28,31,30,31,30,31,31,30,31,30,31 };
int year[100001];
int y, m, d;

bool isOld(int year,int month,int day)
{
	return year < 1752 || (year == 1752 && month < 9) || (year == 1752 && month == 9 && day <= 2);
}

bool isLeap(int year, bool old)
{
	if (old)
		return year % 4 == 0;
	else
		return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}

bool judge(int y, int m, int d)
{
	if (y == 1752 && m == 9 && 3 <= d && d <= 13)
		return false;
	if (y > 0 && 1 <= m && m <= 12)
	{
		if (isLeap(y, isOld(y, 1, 1)))
			days[2] = 29;
		return 1 <= d && d <= days[m];
	}
	return false;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int i;
	int sum = 0;
	int weeks;
	bool old;
	for (i = 1; i <= 9999; ++i)
	{
		if (i == 1752)
			year[i] = year[i - 1] + 355;
		else if (isLeap(i, isOld(i, 1, 1)))
			year[i] = year[i - 1] + 366;
		else
			year[i] = year[i - 1] + 365;
	}
	while (cin >> m >> d >> y, m != 0 || d != 0 || y != 0)
	{
		days[2] = 28; days[9] = 30;
		if (!judge(y, m, d))
		{
			cout << m << "/" << d << "/" << y << " is an invalid date." << endl;
			continue;
		}
		old = isOld(y, m, d);
		switch (m)
		{
		case 1:
			cout << "January " << d << ", " << y;
			break;
		case 2:
			cout << "February " << d << ", " << y;
			break;
		case 3:
			cout << "March " << d << ", " << y;
			break;
		case 4:
			cout << "April " << d << ", " << y;
			break;
		case 5:
			cout << "May " << d << ", " << y;
			break;
		case 6:
			cout << "June " << d << ", " << y;
			break;
		case 7:
			cout << "July " << d << ", " << y;
			break;
		case 8:
			cout << "August " << d << ", " << y;
			break;
		case 9:
			cout << "September " << d << ", " << y;
			break;
		case 10:
			cout << "October " << d << ", " << y;
			break;
		case 11:
			cout << "November " << d << ", " << y;
			break;
		case 12:
			cout << "December " << d << ", " << y;
			break;
		}
		sum = 0;
		sum = year[y - 1];
		if (isLeap(y, old))days[2] = 29;
		if (y == 1752)days[9] = 19;
		for (i = 1; i < m; ++i)sum += days[i];
		if (y == 1752 && m == 9 && d >= 14)
			sum = sum + (d - 11);
		else 
			sum += d;
		weeks = sum % 7;
		switch (weeks)
		{
		case 0:
			cout << " is a Friday" << endl;
			break;
		case 1:
			cout << " is a Saturday" << endl;
			break;
		case 2:
			cout << " is a Sunday" << endl;
			break;
		case 3:
			cout << " is a Monday" << endl;
			break;
		case 4:
			cout << " is a Tuesday" << endl;
			break;
		case 5:
			cout << " is a Wednesday" << endl;
			break;
		case 6:
			cout << " is a Thursday" << endl;
			break;
		}
	}
	return 0;
}

你可能感兴趣的:(日历问题)