What Day Is It?

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.


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

当出现不应该有的日期时输出, 月/日 /年 /  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<stdio.h>
#include<string.h>
#include<stdlib.h>

char* m_change(int month)//把月转换成英文
{
	char *str;
	switch (month)
	{
	case 1:
		str = "January";
		break;
	case 2:
		str = "February";
		break;
	case 3:
		str = "March";
		break;
	case 4:
		str = "April";
		break;
	case 5:
		str = "May";
		break;
	case 6:
		str = "June";
		break;
	case 7:
		str = "July";
		break;
	case 8:
		str = "August";
		break;
	case 9:
		str = "September";
		break;
	case 10:
		str = "October";
		break;
	case 11:
		str = "November";
		break;
	case 12:
		str = "December";
		break;

	}
	return str;
}



int week_change(int month, int day, int year)
{
	int s = 0;
	if (month == 1) { month = 13; year--; }
	if (month == 2) { month = 14; year--; }
	if ((year < 1752) || ((year == 1752) && (month < 9)) || ((year == 1752) && (month == 9) && (day < 3)))
	{
		s = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 + 5) % 7;
	}
	else
	{
		s = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
	}
	return s;


}
char* s_change(int s)
{
	char *str;
	switch (s)
	{
	case 0:
		str = "Monday";
		break;
	case 1:
		str = "Tuesday";
		break;
	case 2:
		str = "Wednesday";
		break;
	case 3:
		str = "Thursday";
		break;
	case 4:
		str = "Friday";
		break;
	case 5:
		str = "Saturday";
		break;
	case 6:
		str = "Sunday";
		break;
	}
	return str;
}


int check_ord_day(int month, int year)
{
	int num;
	switch (month)
	{
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
		num = 31;
		break;
	case 2:
		if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
			return num = 29;
		else
			return num = 28;
		break;
	case 4:
	case 6:
	case 9:
	case 11:
		num = 30;
		break;
	}
	return num;
}
int check(int month, int day, int year)
{
	if (year <= 0 || month <= 0 || month > 12 || day <= 0 || day > check_ord_day(month, year))
		return 0;
	if ((year == 1752 && month == 9 && 3 <= day && day <= 13))
		return 0;
}
int main()
{
	int month, day, year;
	while (1)
	{
		scanf("%d%d%d", &month, &day, &year);
		if (month == 0 || day == 0 || year == 0)
			break;
		if (check(month, day, year))
		{
			char *str;
			str = m_change(month); //把月转换成英文

			int s;//计算出的星期数
			s = week_change(month, day, year);

			char *ss; //把星期数转换成英文
			ss = s_change(s);

			printf("%s %d, %d is a %s\n", str, day, year, ss);
		}
		else
		{
			printf("%d/%d/%d is an invalid date.\n", month, day, year);
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值