您的位置:首页 > 其它

hdu 6112 今夕何夕(日期计算——蔡勒公式)

2017-08-14 09:44 309 查看

今夕何夕

Problem Description

今天是2017年8月6日,农历闰六月十五。

小度独自凭栏,望着一轮圆月,发出了“今夕何夕,见此良人”的寂寞感慨。

为了排遣郁结,它决定思考一个数学问题:接下来最近的哪一年里的同一个日子,和今天的星期数一样?比如今天是8月6日,星期日。下一个也是星期日的8月6日发生在2023年。

小贴士:在公历中,能被4整除但不能被100整除,或能被400整除的年份即为闰年。

Input

第一行为T,表示输入数据组数。

每组数据包含一个日期,格式为YYYY-MM-DD。

1 ≤ T ≤ 10000

YYYY ≥ 2017

日期一定是个合法的日期

Output

对每组数据输出答案年份,题目保证答案不会超过四位数。

Sample Input

3

2017-08-06

2017-08-07

2018-01-01

Sample Output

2023

2023

2024

思路:蔡勒公式

注意特判2月29,2月29要4年4年的加

代码:

#include<stdio.h>

bool leap(int y)
{
if((y%4==0&&y%100!=0)||y%400==0)
return true;
return false;
}

int Zeller(int y,int m,int d)
{
if(m<=2)
m+=12,--y;
int a=y/100,b=y%100;
int w=(a/4-2*a+b+b/4+13*(m+1)/5+d-1+700)%7;
return w;
}

int main()
{
int t,y,m,d;
scanf("%d",&t);
while(t--)
{
scanf("%d-%d-%d",&y,&m,&d);
int w=Zeller(y,m,d);
if(m==2&&d==29)
{
for(int i=y+4; i<=9999; i+=4)
if(leap(i))
{
if(Zeller(i,m,d)==w)
{
printf("%d\n",i);
break;
}
}
}
else
{
for(int i=y+1; i<=9999; ++i)
if(Zeller(i,m,d)==w)
{
printf("%d\n",i);
break;
}
}
}
return 0;
}


ps:还可以模拟着写

代码:

#include<stdio.h>

int leap(int y)
{
if((y%4==0&&y%100!=0)||y%400==0)
return 1;
return 0;
}

int main()
{
int t,y,m,d;
scanf("%d",&t);
while(t--)
{
scanf("%d-%d-%d",&y,&m,&d);
int tot=0;
if(m==2&&d==29)
{
while(1)
{
y+=4;
tot=(tot+365*4+leap(y))%7;
if(!tot&&leap(y))//注意这里要判断闰年
break;
}
}
else
{
while(1)
{
if(m>2)
tot=(tot+365+leap(y+1))%7;
else
tot=(tot+365+leap(y))%7;
++y;
if(!tot)
break;
}
}
printf("%d\n",y);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: