NYOJ 219 An problem about date(暴力)

描述

acm的iphxer经常忘记某天是星期几,但是他记那天的具体日期,他希望你能写个程序帮帮他。

 

输入
每行有三个整数 year,month,day,日期在1600年1月1日到9600年1月1日之间;
输出
输出对应的星期,用一个整数表示;(星期一到星期六用1-6表示,星期日用0表示)
样例输入
2011 3 6
1949 10 1
2011 4 1
1945 8 15
样例输出
0
6
5
3


下面是第一次提交的代码,提示wa,但测试样例都没有问题。

#include <cstdio>  
#define ISYEAP(x) x%4==0&&x%100!=0||x%400==0?1:0  
int dayOfMonth[13][2]={  
    0,0,  
    31,31,  
    28,29,  
    31,31,  
    30,30,  
    31,31,  
    30,30,  
    31,31,  
    31,31,  
    30,30,  
    31,31,  
    30,30,  
    31,31  
};  
struct date  
{  
    int year;  
    int month;  
    int day;  
    void nextDay()  
    {  
        day++;  
        if(day>dayOfMonth[month][ISYEAP(year)])  
        {  
            month++;  
            day=1;  
            if(month>12)  
            {  
                year++;  
                month=1;  
            }  
        }  
    }  
};  
int buf[9601][13][32];  
int main()  
{  
    date nor;  
    nor.year=0;  
    nor.month=1;  
    nor.day=1;  
    int x=0;  
    while(nor.year!=9601)  
    {  
        buf[nor.year][nor.month][nor.day]=x;  
        x++;  
        nor.nextDay();  
    }  
    int year;  
    int month;  
    int day;  
    while(scanf("%d%d%d",&year,&month,&day)!=EOF)  
    {  
        int days=buf[year][month][day]-buf[2011][3][6];  
        printf("%d\n",(days%7+7)%7);  
    }  
    return 0;  
}  

感谢优秀的lzy学长帮忙,后来才明白,nyoj219中的数据有问题,就类似1900-2-29(其实1900年是平年,并没有2-29)这样的输入也需要一个“合理”的输出,我之前的代码对于非法输入是不计算的,所以没过,下面是针对特殊数据的ac代码:

#include <cstdio>
#define ISYEAR(x) (x%4==0&&x%100!=0)||(x%400==0)?1:0
int DayOfMonth[13]
{
    0,
    31,
    28,
    31,
    30,
    31,
    30,
    31,
    31,
    30,
    31,
    30,
    31
};
struct Date
{
    int year;
    int month;
    int day;
    void NextDay()
    {
        day++;
        if(day>DayOfMonth[month]+(month==2?ISYEAR(year):0))
        {
            month++;
            day=1;
        }
        if(month>12)
        {
            year++;
            month=1;
            day=1;
        }
    }
};
char inf[10000][13][32];
int main()
{
    Date index;
    int x=6;
    index.day=1;
    index.month=1;
    index.year=1600;
    while(index.year<10000)
    {
        inf[index.year][index.month][index.day]=x;
        x=(x+1)%7;
        index.NextDay();
    }
    int YEAR;
    int MONTH;
    int DAY;
    while(scanf("%d%d%d",&YEAR,&MONTH,&DAY)!=EOF)
    {
        int n;
        if(DAY>2)
            n=(inf[YEAR][MONTH][DAY-2]+2)%7;
        else
            n=inf[YEAR][MONTH][DAY];
        printf("%d\n",n);
    }
    return 0;
}    
 

不仅理解了wa的缘由,也将之前的代码做了优化,比如将二维数组压成一维,将打表的三维数组数据类型从int改为char,直接缩小一倍内存。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值