【HDU 6112 今夕何夕】

本文介绍了一个算法问题,即如何找到下一个具有相同星期数的特定日期。通过分析日期和考虑闰年因素,该问题提供了详细的解决方案及示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今夕何夕
Accepts: 1345 Submissions: 5533
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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,判断下最后找到的年份是否为闰年

AC代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX = 1e5 + 10;
typedef long long LL;
bool fu(int n){
    if(n % 400 == 0) return true;
    else if(n % 100 == 0) return false;
    else if(n % 4 == 0) return true;
    return false;
}
void solve(int a,int b,int c){
    int ans = 0,o = a;
    if(b == 2 && c == 29){
        while(1){
            o += 4;
            if(fu(o)) ans += 365 * 3 + 366;
            else ans += 365 * 4;
            ans %= 7;
            if(fu(o) && ans == 0){
                printf("%d\n",o); return ;
            }
        }
    }
    else if(b >= 3){
        while(1){
            o++;
            if(fu(o)) ans += 366;
            else ans += 365;
            ans %= 7;
            if(ans == 0){
                printf("%d\n",o);
                return ;
            }
        }
    }
    else{
        while(1){
            if(fu(o)) ans += 366;
            else ans += 365;
            o++;
            ans %= 7;
            if(ans == 0){
                printf("%d\n",o);
                return ;
            }
        }
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        int a,b,c;
        scanf("%d-%d-%d",&a,&b,&c);
        solve(a,b,c);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值