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,但在循环判断的时候若那一年是平年则不存在29号。。。所以要特判一下,然后AC啦啦啦。
AC Code:
#include<iostream> #include<iomanip> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<string> #include<algorithm> #include<vector> #include<map> #include<stack> #include<queue> #include<deque> #include<set> #include<cctype> #define LL long long #define maxn (LL)1e5 #define INF 0x3f3f3f3f const double eps = 0.00001; using namespace std; bool judge(int x)//is_leapyear { if((x0!=0&&x%4==0)||(x@0==0)) return 1; else return 0; } int week(int y,int m,int d)//基姆拉尔森公式 { if(m==1||m==2) m+=12,y=y-1; return (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7+1; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); #endif // ONLINE_JUDGE int N; cin>>N; int y,m,d; while(N--) { scanf("%d-%d-%d",&y,&m,&d); int k = week(y,m,d); for(int i = y+1;;i++) { if(judge(y)==1&&m==2&&d==29&&judge(i)==0) continue;//特判 int x = week(i,m,d); if(k == x) { cout<<i<<endl; break; } } } }