HDU 6112 今夕何夕 【数学公式】 (2017"百度之星"程序设计大赛 - 初赛(A))

63 篇文章 0 订阅
28 篇文章 0 订阅

今夕何夕

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1295    Accepted Submission(s): 455


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
 

Source
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6113  6112  6111  6110  6109 
 

Statistic |  Submit |  Discuss |  Note




题目链接:

  http://acm.hdu.edu.cn/showproblem.php?pid=6112

题目大意:

  给定日期,求下一个同月同日且为同星期的年份。

题目思路:

  【公式】

  利用蔡勒公式(见代码)求解某一具体日期的星期,往后枚举即可。(注意闰年2月29日)




/****************************************************

	Author : Coolxxx
	Copyright 2017 by Coolxxx. All rights reserved.
	BLOG : http://blog.csdn.net/u010568270

****************************************************/
#include<bits/stdc++.h>
#pragma comment(linker,"/STACK:1024000000,1024000000")
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define mem(a,b) memset(a,b,sizeof(a))
const double EPS=0.00001;
const int J=10;
const int MOD=100000007;
const int MAX=0x7f7f7f7f;
const double PI=3.14159265358979323;
const int N=104;
const int M=1004;
using namespace std;
typedef long long LL;
double anss;
LL aans;
int cas,cass;
int n,m,lll,ans;
int week(int year,int month,int day)
{
	if(month<3)
	{
		year-=1;
		month+=12;
	}
	int c=int(year/100),y=year-100*c;
	int w=int(c/4)-2*c+y+int(y/4)+(26*(month+1)/10)+day-1;
	w=(w%7+7)%7;
	return w;
}
int week1(int y,int m,int d)
{
	if(m==1) m=13,y--;
	if(m==2) m=14,y--;
	int week=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;
	return week;
}
int main()
{
	#ifndef ONLINE_JUDGE
//	freopen("1.txt","r",stdin);
//	freopen("2.txt","w",stdout);
	#endif
	int i,j,k;
	int x,y,z;
//	for(scanf("%d",&cass);cass;cass--)
	for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
//	while(~scanf("%d",&n))
	{
		int year,month,day;
		scanf("%d-%d-%d",&year,&month,&day);
		x=week(year,month,day);
		if(month==2 && day==29)
			z=4;
		else z=1;
		for(i=year+z;i<10000;i+=z)
		{
			if(z==4 && !((i%4==0 && i%100!=0) || (i%400==0)))
				continue;
			y=week(i,month,day);
			if(y==x)break;
		}
		printf("%d\n",i);
	}
	return 0;
}
/*
//

//
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值