1370: What Day Is It? 判断某天是星期几

本文介绍了一个程序,该程序能够将美国使用的历法中的日期转换为对应的星期几。文章详细解释了儒略历和格里高利历的规则,并提供了一个示例程序来处理不同年份的闰年情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1370: What Day Is It?


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s8192K14542Standard

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.

 

 

 


This problem is used for contest: 137 

 

 

 

#include <iostream>
#include <string>
using namespace std;

const string weeks[]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
const string months[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
const int cnt[2][12]=
{
31,28,31,30,31,30,31,31,30,31,30,31,
   31,29,31,30,31,30,31,31,30,31,30,31
};

int leapyear(int y)
{
return y%4==0 && y%100!=0 || y%400==0;
}

int DayOfWeek(int M,int D,int Y)
{
if(M==1 || M==2)
{
   M+=12;
   --Y;
}
if(Y<1752 || Y==1752 && M<9 || Y==1752 && M==9 && D<3)
   return (D+2*M+3*(M+1)/5+Y+Y/4+5)%7;
else
   return (D+2*M+3*(M+1)/5+Y+Y/4-Y/100+Y/400)%7;
}

bool Check(int m,int d,int y)
{
if(! (m>=1 && m<13)) return false;
if(! (d>=1 && d<=cnt[leapyear(y)][m-1])) return false;
if(y==1752 && m==9 && d>2 && d<14) return false;
return true;
}

int main()
{
int m,d,y;
while(cin>>m>>d>>y,m+d+y)
{
   if(Check(m,d,y))
    printf("%s %d, %d is a %s/n",months[m-1].c_str(),d,y,weeks[DayOfWeek(m,d,y)].c_str());
   else
    printf("%d/%d/%d is an invalid date./n",m,d,y);
}
return 0;
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值