edu13 B. The Same Calendar

B. The Same Calendar

题意:给一个year ,  求最接近的一年,使得这一年的日历表和该年一样

思路:用zeller公式判断每年的1月1号是否日期相同,若相同,只要接下来一年的时间相同(闰/非闰年的差别)就可以判断是否相同. 妙

#include<bits/stdc++.h>
#define PI acos(-1.0)
#define pb push_back
#define F first
#define S second
#define debug puts
#define setp cout << fixed << setprecision(3)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
const int N=1e6+6;
const int MOD=1e9+7;
const ll INF=1e18+8;

int check(int year,int month,int day){
    if(month==1||month==2){
        month+=12;
        year--;
    }
    int c=year/100;
    int y=year%100;
    int m=month;
    int d=day;
    int W=c/4-2*c+y+y/4+26*(m+1)/10+d-1;
    if(W<0) return (W+(-W/7+1)*7)%7;
    return W%7;
}
bool check1(int y){
    if(y%400==0 || y%100!=0&&y%4==0)    return true;
    return false;
}
int main(void){
    FAST_IO;
    int y;
    cin >> y;
    int d=check(y,1,1);
    if(check1(y)){
        while(1){
            y++;
            if(check1(y)&&check(y,1,1)==d){
                cout << y << endl;
                return 0;
            }
        }
    }
    else{
        while(1){
            y++;
            if(!check1(y)&&check(y,1,1)==d){
                cout << y << endl;
                return 0;
            }
        }
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值