Official

A - Leap Year Editorial by en_translator


For beginners

Let us use an if statement to implement the casework in the problem statement. To determine if \(x\) is divisible by \(y\), one can check if x % y == 0. Here, x % y denotes the remainder when \(x\) is divided by \(y\), where % is the operator that finds the remainder.

When \(x\) is divisible by \(y\), the remainder when \(x\) is divided by \(y\) is \(0\), so the code above determines the divisibility.

In our case, there are multiple if statements, possible making the code deeply nested. In such case, one can immediately print the result and return once a condition is met to avoid deep nests.

Sample code (C++):

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

int main() {
    int y;
    cin >> y;
    if(y % 4) {
        cout << 365 << endl;
        return 0;
    }
    if(y % 100) {
        cout << 366 << endl;
        return 0;
    }
    if(y % 400) {
        cout << 365 << endl;
        return 0;
    }
    cout << 366 << endl;
    return 0;
}

posted:
last update: