POJ2080 ZOJ2420 Calendar

//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
请按任意键继续. . .
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值