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. 

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<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
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]<day)||(month==2&&!judge(year)&&months[month]<day)||(month==2&&judge(year)&&months[month]+1<day)||(1752==year&&month==9&&(day>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;i<year;i++)
			{
				if(judge(i)==1)
				day2+=366;
				else
				day2+=365;
			}
			for(i=1;i<month;i++)
			day2+=months[i];
			if(judge(year)&&month>2)
			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);
		}
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章