回 帖 发 新 帖 刷新版面

主题:经高手给了一些测试数据后对了,但提交时WA,555555

唉!在PKU里,这道题(2080--Calendar)做了很久,但终不得解,老是提交WA,再次麻烦高手帮帮忙,谢谢先!
(下附原体和我的代码)

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;
}

回复列表 (共14个回复)

11 楼

对阿,好奇怪.12是肯定要的.再加一个0的,那13个月也要的.刚开始写代码时,边界不好控制(那时还分不太清楚),所以要了14个.怎么是15个月,那我也不清楚了,可能当时恍惚了吧!

12 楼

你就老实写成12个月的样子慢慢-就可以了
一个一个对容易自己+错天数

13 楼

最后最后又发现你的问题了
这应该是最后的问题了{0,31,60,91,121,152,183,213,244,274,305,335,366,377};
{0,31,59,90,120,151,182,212,243,275,304,334,365,377}
还是应了我说的
你计算错了
151是1月到5月的天数
[color=ff0000]你把6月算了31天7月算成30天了,这样整个7月的日期全是错的,当然是wa!!151之后应该是181,后面212[/color=ff0000]

14 楼

对,我真的是错的不行啊.真的很佩服你.唉,我真的太粗心了;路呀,还是要仔仔细细地走,稍不甚,全盘皆完蛋.再次衷心地感谢你!

我来回复

您尚未登录,请登录后再回复。点此登录或注册