zzulioj - 2575: 计算日期

水题(然而还是没做出来,wttmcl),就是简单模拟
(之前写的特判有问题,修改了一下)

#include<cstdio>
#include<stack>
#include<queue>
#include<cmath>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#define TP 233333333
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int maxn = 1005;
bool is_run(int x) {
    return (!(x%4) && x%100) || !(x%400); // 判断闰年
}
int main(void) {
    int year, month, day;
    while(cin >> year >> month >> day) {
        int sum = 0;
        if (month == 2 && day == 29) { //日期为2月29时要判断明年是否是闰年,同时最后符合条件的年份必定是闰年
            while( !(!(sum%7) && is_run(year)) || !sum)
                sum += is_run(++year) ? 366 : 365;
        }
        else {
            while(!sum || sum%7) {
                if (month <= 2)
                    sum += is_run(year++) ? 366 : 365; //因为上面特判已经排除2月29号, 所以闰年时有两种情况
                else                                   //月份小于等于二月 比如2月25号, 再加一年的时候会加上今年多闰出来的的2月29号
                    sum += is_run(++year) ? 366 : 365; //月份大于二月, 比如3月25号, 再加一年的时候会加上明年多闰出来的2月29号
            }
        }
        cout << year << endl;
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值