HDU 6112 今夕何夕

今夕何夕

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


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
/*
* @Author: lyuc
* @Date:   2017-08-12 14:56:45
* @Last Modified by:   lyuc
* @Last Modified time: 2017-08-13 21:35:47
*/

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>

#define LL long long
#define INF 0x3f3f3f3f

using namespace std;

int n;
int y,m,d;
int D;
int cnt;

bool IsLeap(int year)
{
   return (year % 4 ==0 && year % 100 !=0) || year % 400 ==0;
}

int main(){
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    scanf("%d",&n);
    while(n--){
        scanf("%d-%d-%d",&y,&m,&d);
        if(m==1){
            D=d;
            cnt=D;
        }else if(m==2){
            D=31+d;
            cnt=D;
        }else{
            D=13*(m+1)/5-7+(m-1)*28+d;
            cnt=D;
            if(IsLeap(y)==true)
                D++;
        }
        int pos=((y-1)*365+(y-1)/4-(y-1)/100+(y-1)/400+D)%7;//蔡勒公式

        int i=y;
        while(i++){
            if(IsLeap(i)==false&&m==2&&d==29) continue;
            int res=(i-1)*365+(i-1)/4-(i-1)/100+(i-1)/400;
            if(IsLeap(i)==true&&m>=3){
                res+=cnt+1;
            }else{
                res+=cnt;
            }
            if(res%7==pos){
                printf("%d\n",i);
                break;
            }
        }
    }
    return 0;
}

 

 

 

转载于:https://www.cnblogs.com/wuwangchuxin0924/p/7354945.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值