您的位置:首页 > 其它

An problem about date(数学 )

2015-04-23 16:37 190 查看


An problem about date

时间限制:2000 ms | 内存限制:65535 KB
难度:2

描述

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


code:

公式:k=(Day +1+ 2*Month + 3*(Month+1)/5 + Year + Year/4 - Year/100 + Year/400) % 7;

需要注意的地方就是1月和2月算作上年的13月,和14月。

#include<stdio.h>
int main()
{
    int year,mouth,day,k,ans;
    while(scanf("%d %d %d",&year,&mouth,&day)!=EOF)
    {
        if(mouth==1)
        {
            mouth=13;year-=1;
        }
        if(mouth==2)
        {
            mouth=14;year-=1;
        }
        k=1+day+2*mouth+3*(mouth+1)/5+year/400-year/100+year+year/4;
        ans=k%7;
        printf("%d\n",ans);
    }
    return 0;
}
数学是神造的科学!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: