Facile 0 risolte 0% di accettazione

Leap Year

Check whether a year follows the Gregorian leap-year rule.

Cosa imparerai

  • Combine divisibility rules with and/or
  • Apply the century exception
  • Write a clear one-line condition

Problema

Mostra traduzione

A year divisible by 4 is a leap year unless it is also divisible by 100. Years divisible by 400 are leap years despite that exception. Apply this arithmetic rule to every allowed input year.

Implement leap_year(year).

Input

year: The year to check.

Output

Return true for a leap year and false otherwise.

Vincoli

1 ≤ year ≤ 9999.

Esempi

Esempio 1
Input:
year = 2024

Output atteso:
expected = True
Input:
long long year = 2024;

Output atteso:
bool expected = true;
Input:
long year = 2024;

Output atteso:
bool expected = true;

Spiegazione

2024 is divisible by 4 and not by 100.

Esempio 2
Input:
year = 2100

Output atteso:
expected = False
Input:
long long year = 2100;

Output atteso:
bool expected = false;
Input:
long year = 2100;

Output atteso:
bool expected = false;

Spiegazione

2100 is divisible by 100 but not by 400, so the century exception applies.

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

Most years follow the divisible-by-4 rule, but centuries are exceptions.

Suggerimento 2

A year divisible by 400 is still a leap year, even though it is also divisible by 100.

Suggerimento 3

Check the exception before rejecting century years, or you may misclassify years like 2000.

Complessità attesa

O(1) time, O(1) extra space

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

Impara dagli altri

Soluzioni della community

Nessuna soluzione pubblicata. Risolvi questa sfida per condividere la prima.