主题:经高手给了一些测试数据后对了,但提交时WA,555555
(下附原体和我的代码)
Calendar
Time Limit:1000MS Memory Limit:30000K
Total Submit:1601 Accepted:591
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
Source
Shanghai 2004 Preliminary
#include <iostream.h>
#include<iomanip.h>
int main()
{
char week[7][10]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
long fh[26],hh[6],y[6]={0,366,731,1096,1461,1468},days;
int dleap[15]={0,31,60,91,121,152,183,213,244,274,305,335,366,377};
//若今年是闰年,dleap[i]表示从开头到第i+1月为止,过了多少天
int dfl[15]={0,31,59,90,120,151,182,212,243,275,304,334,365,377};
//若今年不是闰年,dfl[i]表示从开头到第i+1月为止,过了多少天
int i,j,k,p,year,s,weekth,leap=0,q,date;
for(i=0;i<26;i++)//确定第i个400年内的总天数
fh[i]=i*146100;
for(i=0;i<5;i++)
hh[i]=i*36525;//确定第i个世纪内的总天数
cin>>days;
while(days!=-1)
{
days++;
for(i=0;i<26;i++)
//确定days在第几个400年内,若days<fh[i+1],则days在第i个400年内
if(days<=fh[i+1])
break;
days=days-fh[i];
for(j=0;j<5;j++)
//确定days在第几个世纪内,若days<hh[j+1],则days在第j个世纪内
if(days<=hh[j+1])
break;
days=days-hh[j];
for(k=0;k<27;k++)//确定days在第几个4年内,若days<1461*(k+1),则says在第k个四年内
if(days<=1461*(k+1))
break;
days=days-1461*k;
for(p=0;p<6;p++)//确定days在第几年内,若days<y[p+1],则days在dip个年内
if(days<=y[p+1])
break;
days=days-y[p];//days在本年的第几天.
year=2000+i*400+j*100+k*4+p;//year就是本年份的年值
if(year%4==0&&year%100!=0||year%4==0&&year%100==0)
leap=1;
if(leap)//确定是第几个月
for(q=0;q<15;q++)
{
if(days<=dleap[q+1])
{
date=days-dleap[q];
break;
}
}
else
for(q=0;q<15;q++)
{
if(days<=dfl[q+1])
{
date=days-dfl[q];
break;
}
}
q++;//月份
s=year-1+(year-1)/4-(year-1)/100+(year-1)/400+days;
weekth=s%7;//weekth就是星期数
//printf("%d-%d-%d %s\n",year,q,date,week[weekth]);
cout<<year<<'-'<<setfill('0')<<setw(2)<<q<<'-'<<setfill('0')<<setw(2)<<date<<' '<<week[weekth]<<endl;
cin>>days;
}
return 1;
}