Calendar
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 12347 | Accepted: 4480 |
Description
A calendar is a system for measuring time, from hours and minutes, to months and days, and finally to years and centuries. The terms of hour, day, month, year and century are all units of time measurements of a calender system.
According to the Gregorian calendar, which is the civil calendar in use today, years evenly divisible by 4 are leap years, with the exception of centurial years that are not evenly divisible by 400. Therefore, the years 1700, 1800, 1900 and 2100 are not leap years, but 1600, 2000, and 2400 are leap years.
Given the number of days that have elapsed since January 1, 2000 A.D, your mission is to find the date and the day of the week.
According to the Gregorian calendar, which is the civil calendar in use today, years evenly divisible by 4 are leap years, with the exception of centurial years that are not evenly divisible by 400. Therefore, the years 1700, 1800, 1900 and 2100 are not leap years, but 1600, 2000, and 2400 are leap years.
Given the number of days that have elapsed since January 1, 2000 A.D, your mission is to find the date and the day of the week.
Input
The input consists of lines each containing a positive integer, which is the number of days that have elapsed since January 1, 2000 A.D. The last line contains an integer −1, which should not be processed.
You may assume that the resulting date won’t be after the year 9999.
You may assume that the resulting date won’t be after the year 9999.
Output
For each test case, output one line containing the date and the day of the week in the format of "YYYY-MM-DD DayOfWeek", where "DayOfWeek" must be one of "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" and "Saturday".
Sample Input
1730 1740 1750 1751 -1
Sample Output
2004-09-26 Sunday 2004-10-06 Wednesday 2004-10-16 Saturday 2004-10-17 Sunday
Source
Shanghai 2004 Preliminary
面向对象方法编的:
一般方法编的:
面向对象方法编的:
/*
*Copyright (c)2015,烟台大学计算机与控制工程学院
*All rights reserved.
*作 者:单昕昕
*完成日期:2015年8月16日
*版 本 号:v1.0
*/
#include <iostream>
using namespace std;
class Time
{
public:
static int year;
static int month;
static int day;
};
int Time::year=2000;
int Time::month=1;
int Time::day=1;
int daysOfMonth(int m,int y)
{
int days;
switch(m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days=31;
break;
case 4:
case 6:
case 9:
case 11:
days=30;
break;
case 2:
if((y%4==0&&y%100!=0)||y%400==0)
days=29;
else
days=28;
}
return days;
}
int dayOfYear(Time t)
{
Time t1=t;
int days=0;
int m=1;
while(m<t1.month)
{
days+=daysOfMonth(m,t1.year);
++m;
}
days+=t1.day;
return days;
}
int daysOfYear(int y)
{
return ((y%4==0&&y%100!=0)||y%400==0)?366:365;
}
Time afterDays(Time t,int d)
{
Time t1=t;
int d1=d+dayOfYear(t);
t1.month=1;
t1.day=0;
while(d1>daysOfYear(t1.year))
{
d1-=daysOfYear(t1.year);
++t1.year;
}
while(d1>daysOfMonth(t1.month,t1.year))
{
d1-=daysOfMonth(t1.month,t1.year);
++t1.month;
}
t1.day+=d1;
return t1;
}
int main()
{
Time t,nt;
int d,s;
while(cin>>d&&d!=-1)
{
s=d%7;
nt=afterDays(t,d);
cout<<nt.year<<"-";
if(nt.month<10)
cout<<"0";
cout<<nt.month<<"-";
if(nt.day<10)
cout<<"0";
cout<<nt.day<<" ";
switch(s)
{
case 0:
cout<<"Saturday"<<endl;
break;
case 1:
cout<<"Sunday"<<endl;
break;
case 2:
cout<<"Monday"<<endl;
break;
case 3:
cout<<"Tuesday"<<endl;
break;
case 4:
cout<<"Wednesday"<<endl;
break;
case 5:
cout<<"Thursday"<<endl;
break;
case 6:
cout<<"Friday"<<endl;
break;
}
nt.year=2000;
nt.month=1;
nt.day=1;
}
return 0;
}
一般方法编的:
/*
*Copyright (c)2015,烟台大学计算机与控制工程学院
*All rights reserved.
*作 者:单昕昕
*完成日期:2015年8月16日
*版 本 号:v1.0
*/
#include <iostream>
using namespace std;
int dayofyear(int y)
{
if((y%4==0&&y%100!=0)||y%400==0)
return 366;
else
return 365;
}
int daysofmonth(int m,int y)
{
int days;
switch(m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days=31;
break;
case 4:
case 6:
case 9:
case 11:
days=30;
break;
case 2:
if((y%4==0&&y%100!=0)||y%400==0)
days=29;
else
days=28;
}
return days;
}
int main()
{
int n,week,year,month,day;
while(cin>>n&&n!=-1)
{
year=2000;
month=1;
day=1;
week=n%7;
while(n)
{
if(n>=dayofyear(year))
{
n-=dayofyear(year);
++year;
}
else if(n>=daysofmonth(month,year))
{
n-=daysofmonth(month,year);
++month;
}
else
{
day+=n;
n=0;
}
}
cout<<year<<"-";
if(month<10)
cout<<"0";
cout<<month<<"-";
if(day<10)
cout<<"0";
cout<<day<<" ";
switch(week)
{
case 0:
cout<<"Saturday"<<endl;
break;
case 1:
cout<<"Sunday"<<endl;
break;
case 2:
cout<<"Monday"<<endl;
break;
case 3:
cout<<"Tuesday"<<endl;
break;
case 4:
cout<<"Wednesday"<<endl;
break;
case 5:
cout<<"Thursday"<<endl;
break;
case 6:
cout<<"Friday"<<endl;
break;
}
}
return 0;
}