1 条题解

  • 0
    @ 2056-2-29 23:30:49

    【深基3.例3】闰年判断

    解题思路

    判断年份能被 400 整除,或者能被 4 整除且不能被 100 整除。

    正确性说明

    公历闰年规则正是四年一闰、百年不闰、四百年再闰,上述表达式完整表示三个层次。

    易错点

    不能只看能否整除 4;1900 与 2000 是关键边界。

    复杂度

    时间 O(1),空间 O(1)。

    参考代码(C++11)

    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int n;
        cin >> n;
        cout << (n % 400 == 0 || (n % 4 == 0 && n % 100 != 0)) << "\n";
        return 0;
    }
    
    • 1