POJ 2080 Calendar C++版

POJ题解 3060浏览

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.

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.

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

Slyar:告诉你今天是2000-1-1，问你N天之后是哪年哪月哪日，星期几，月份和天数不满10的要补0。模拟就好了，细心一点。注意424、425这种数据。

```#include
#include

using namespace std;

string week[] = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
int day[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

/* 判断是否闰年 */
bool isleap(int& year)
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) return true;
else return false;
}

/* 返回一年的最大天数 */
int maxday(int& year)
{
if (isleap(year)) return 366;
else return 365;
}

/* 得到年份 */
int getyear(int& days)
{
int year = 2000;
while (days > maxday(year))
{
days -= maxday(year);
year++;
}
return year;
}

/* 得到月份 */
int getmonth(int year, int& days)
{
int month = 1;
if (isleap(year)) day[2] = 29;
else day[2] = 28;
while (days > day[month])
{
days -= day[month];
month++;
}
return month;
}

/* 得到天数 */
int getday(int& days)
{
return days;
}

/* 得到星期 */
string getweek(int& days)
{
return week[days % 7];
}

/* 主函数 */
int main()
{
int days;
int y, m, d;
string wk;
while(cin >> days && days != -1)
{
wk = getweek(days);
days++;
y = getyear(days);
m = getmonth(y, days);
d = getday(days);

cout << y << "-";
if (m < 10) cout << 0;
cout << m << "-";
if (d < 10) cout << 0;
cout << d << " " << wk << endl;
}
//system("pause");
return 0;
}
```

网友最新评论 (3)

1. 哈哈......
李艺凡8年前 (2013-08-20)回复
2. 电脑右下角有时间，点开到2000-1-1就可以看到了...
Slyar9年前 (2012-08-23)回复
3. 2000.1.1是周几？？？题目中为什么没说？？？
匿名9年前 (2012-08-21)回复