您的位置:首页 > 编程语言 > C语言/C++

NYOJ题目219-An problem about date

2017-08-23 21:47 232 查看


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


4000

样例输出
0
6
5
3


说实话看到这个题目的时候真的很蒙,因为我完全不知道怎么求具体星期几,然后就发现了这个公式
公式:k=(Day +1+ 2*Month + 3*(Month+1)/5 + Year + Year/4 - Year/100 + Year/400) % 7;

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

#include <cstdio>
#include <iostream>
using namespace std;

int main()
{
int year,mouth,day,k,ans;
while(scanf("%d %d %d",&year,&mouth,&day)!=EOF)
{
if(mouth==1||mouth==2)
{
mouth += 12;
year--;
}
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ 水题