Facile 2 risolte 66.7% 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.

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

# Programmatore Linguaggio Tempo Memoria Voto dell'IA Soluzione
1 KKKaneki Ken Python 1,006 ms - 5/5 Vedi il codice
2 GTGarlic The Destroyer Rust 1,455 ms - 5/5 Vedi il codice

Impara dagli altri

Soluzioni della community

Attenzione spoiler: 2 soluzioni complete sotto Prova prima a risolverla da solo. Puoi comunque sbirciare il codice degli altri sviluppatori e le recensioni AI. Accedi e risolvi la sfida per partecipare alla discussione.