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

第一反应date类型题,直接套模板

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

测试样例都没问题,但就是WA,至今没有解决,后来发现有公式可以直接计算,就拿来用了,对原理有兴趣的可以百度基姆拉尔森公式。下面是AC代码

#include <cstdio>
int CalculateWeekDay(int y,int m,int d)
{
    if(m==1||m==2)
    {
        m+=12;
        y--;
    }
    return (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;
}
int main()
{
    int year,month,day;
    while(scanf("%d%d%d",&year,&month,&day)!=EOF)
    {
        int n=CalculateWeekDay(year,month,day);
        if(n>=0&&n<6)
        {
            printf("%d\n",n+1);
        }
        else
            printf("0\n");
    }
    return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值