更完备的闰年规则与更优雅的代码
查看原帖
更完备的闰年规则与更优雅的代码
100724
moonfair楼主2020/11/25 16:02

闰年规则

目前使用的格里高利历闰年规则如下:

  • 公元年分非4的倍数,为平年,或
  • 公元年分为4的倍数但非100的倍数,为闰年,或
  • 公元年分为100的倍数但非400的倍数,为平年,或
  • 公元年分为400的倍数但非3200的倍数,为闰年,或
  • 公元年分为3200的倍数但非80000年的倍数,为平年,或
  • 公元年分为80000的倍数为闰年。

来源:维基百科

实现代码

#include<bits/stdc++.h>
using namespace std;

bool isLeap(int n)
{
    if(n%4)
        return false;

    if(n%100)
        return true;

    if(n%400)
        return false;

    if(n%3200)
        return true;

    if(n%80000)
        return false;

    return true;
}

int main()
{
    int n;
    cin>>n;

    if(isLeap(n))
        printf("1");
    else
        printf("0");
}

2020/11/25 16:02
加载中...