POJ 2964 日历问题 解题报告
编号:2964
考查点:日期和时间处理,此题是最经典的日期计算问题.
思路:自己一开始的思路是先大致计算出年份,然后根据闰年数对计算出的年份进行处理,得到准确年份和剩余天数,过程相当复杂,而且没有考虑中间闰年数超过一年的情况,改了改去都AC不了,后来参考书上的思路,明白一年一年的处理代码简单N倍,虽然性能差了点..
提交情况: 作这道题花了一下午,中间WA了不下20次,痛苦死,后来改变思路就只WA了一次。
Source Code:
// POJ Grids 2964
#include < iostream >
using namespace std;
int month[] = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 };
int year[] = { 365 , 366 };
char weekday[ 7 ][ 10 ] = { " Saturday " , " Sunday " , " Monday " , " Tuesday " , " Wednesday " , " Thursday " , " Friday " };
int type( int y)
{
if (y % 4 || (y % 100 == 0 && y % 400 ))
{
return 0 ;
}
return 1 ;
}
int main()
{
int days;
cin >> days;
while (days !=- 1 )
{
char * week = weekday[days % 7 ];
int i = 2000 ;
for (;days - year[type(i)] >= 0 ;i ++ )
{
days -= year[type(i)];
}
cout << i << " - " ;
if (type(i))
month[ 1 ] = 29 ;
else
month[ 1 ] = 28 ;
i = 0 ;
for (;days - month[i] >= 0 ;i ++ )
{
days -= month[i];
}
if ( ++ i < 10 )
{
cout << " 0 " ;
}
cout << i << " - " ;
if ( ++ days < 10 )
{
cout << " 0 " ;
}
cout << days << " " << week << endl;
cin >> days;
}
return 0 ;
}
总结:原来经典代码一定也是简短的代码..如果发现自己WA了几十次还不行,赶紧换思路..这道题浪费了很多时间,导致我今天没时间做完第5章了,郁闷
By Ns517
Time 09.01.23