HDU 6122 今夕何夕 【数学公式】 (2017"百度之星"程序设计大赛

今夕何夕

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日)

  

  

 1 /****************************************************
 2 
 3     Author : Coolxxx
 4     Copyright 2017 by Coolxxx. All rights reserved.
 5     BLOG : http://blog.csdn.net/u010568270
 6 
 7 ****************************************************/
 8 #include<bits/stdc++.h>
 9 #pragma comment(linker,"/STACK:1024000000,1024000000")
10 #define abs(a) ((a)>0?(a):(-(a)))
11 #define lowbit(a) (a&(-a))
12 #define sqr(a) ((a)*(a))
13 #define mem(a,b) memset(a,b,sizeof(a))
14 const double EPS=0.00001;
15 const int J=10;
16 const int MOD=100000007;
17 const int MAX=0x7f7f7f7f;
18 const double PI=3.14159265358979323;
19 const int N=104;
20 const int M=1004;
21 using namespace std;
22 typedef long long LL;
23 double anss;
24 LL aans;
25 int cas,cass;
26 int n,m,lll,ans;
27 int week(int year,int month,int day)
28 {
29     if(month<3)
30     {
31         year-=1;
32         month+=12;
33     }
34     int c=int(year/100),y=year-100*c;
35     int w=int(c/4)-2*c+y+int(y/4)+(26*(month+1)/10)+day-1;
36     w=(w%7+7)%7;
37     return w;
38 }
39 int week1(int y,int m,int d)
40 {
41     if(m==1) m=13,y--;
42     if(m==2) m=14,y--;
43     int week=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;
44     return week;
45 }
46 int main()
47 {
48     #ifndef ONLINE_JUDGE
49 //    freopen("1.txt","r",stdin);
50 //    freopen("2.txt","w",stdout);
51     #endif
52     int i,j,k;
53     int x,y,z;
54 //    for(scanf("%d",&cass);cass;cass--)
55     for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
56 //    while(~scanf("%d",&n))
57     {
58         int year,month,day;
59         scanf("%d-%d-%d",&year,&month,&day);
60         x=week(year,month,day);
61         if(month==2 && day==29)
62             z=4;
63         else z=1;
64         for(i=year+z;i<10000;i+=z)
65         {
66             if(z==4 && !((i%4==0 && i%100!=0) || (i%400==0)))
67                 continue;
68             y=week(i,month,day);
69             if(y==x)break;
70         }
71         printf("%d
",i);
72     }
73     return 0;
74 }
75 /*
76 //
77 
78 //
79 */
View Code
原文地址:https://www.cnblogs.com/Coolxxx/p/7352827.html