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.
题意:就是给你一个日子,然后让你输出按照他所给的规则应该是周几。格式上面对于之前的输出也有一定的要求。
分析:这题就是一个比较复杂的模拟,一开始觉得这道题目容易理解容易做,但是还是遇到了很多的小问题,最后调试了很久才通过。
有很多小细节需要注意,细心细心细心,重要的事情说三遍。
#include
#include
#include
#include
#include
using namespace std;
int months[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int judge(int year)
{
if(year<=1752)
{
if(year%4==0)
return 1;
else
return 0;
}
else
{
if(year%4==0&&year%100)
{
return 1;
}
else if(year%4==0&&year%100==0&&year%400==0)
{
return 1;
}
else
{
return 0;
}
}
}
int judge2(int month,int day,int year)
{
if(month>12||(month!=2&&months[month]2&&day<14)))
return 0;
else
return 1;
}
void printmonth(int a)
{
if(a==1)
printf("January");
if(a==2)
printf("February");
if(a==3)
printf("March");
if(a==4)
printf("April");
if(a==5)
printf("May");
if(a==6)
printf("June");
if(a==7)
printf("July");
if(a==8)
printf("August");
if(a==9)
printf("September");
if(a==10)
printf("October");
if(a==11)
printf("November");
if(a==12)
printf("December");
}
void printday(int c)
{
if(c==1)
printf("Monday\n");
if(c==2)
printf("Tuesday\n");
if(c==3)
printf("Wednesday\n");
if(c==4)
printf("Thursday\n");
if(c==5)
printf("Friday\n");
if(c==6)
printf("Saturday\n");
if(c==0)
printf("Sunday\n");
}
int main()
{
int year,month,day,t,flag,day2,i,j,k;
while(scanf("%d %d %d",&month,&day,&year))
{
if(month==0||year==0||day==0)
break;
t=year;
if(judge2(month,day,year)==0)
printf("%d/%d/%d is an invalid date.\n",month,day,year);
else
{
day2=-1;
for(i=1;i2)
day2++;
day2=day2+day-1;
if(year>1752||(year==1752&&month>9)||(year==1752&&month==9&&day>=14))
day2-=11;
day2=day2%7;
printmonth(month);
printf(" %d, %d is a ",day,t);
printday(day2);
}
}
}