//4_1_1: Calendar 从2000年1月1日起给定天数求日期POJ2080,ZOJ2420
#include <iostream>
#include <cstring>
using namespace std;
const char week_str[][10] = {"Saturday","Sunday","Monday",
"Tuesday","Wednesday","Thursday","Friday"};
int days_of_year(int year)
{
if(year % 100 == 0)
return year % 400 == 0 ? 366 : 365;
return year % 4 == 0 ? 366 : 365;
}
int days_of_month(int month,int year)
{
if(month == 2)
return days_of_year(year) == 366 ? 29 : 28;
switch(month)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
return 31;
default:
return 30;
}
}
int main(void)
{
int n;
cin >> n;
while(n >= 0)
{
int year,month,day,week;
week = n % 7;
year = 2000;
month = 1;
day = 1;
while(n)
{
if(n >= days_of_year(year))
n -= days_of_year(year++);
else if(n >= days_of_month(month,year))
n -= days_of_month(month++,year);
else {
day += n;
n = 0;
}
}
cout << year << '-' << (month < 10 ? "0" : "")
<< month << '-' << (day < 10 ? "0" : "")
<< day << ' ' << week_str[week] << endl;
cin >> n;
}
return 0;
}
/*测试结果:通过POJ2080和ZOJ2420检测
1730
2004-09-26 Sunday
1740
2004-10-06 Wednesday
0
2000-01-01 Saturday
10000
2027-05-19 Wednesday
10000
2027-05-19 Wednesday
100000
2273-10-16 Thursday
1000000
4737-11-28 Sunday
-1
请按任意键继续. . .
*/
POJ2080 ZOJ2420 Calendar
最新推荐文章于 2018-01-14 11:24:46 发布