Facile 0 résolus 0 % de réussite

Leap Year

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

Ce que vous allez apprendre

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

Problème

Afficher la traduction

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.

Contraintes

1 ≤ year ≤ 9999.

Exemples

Exemple 1
Entrée :
year = 2024

Sortie attendue :
expected = True
Entrée :
long long year = 2024;

Sortie attendue :
bool expected = true;
Entrée :
long year = 2024;

Sortie attendue :
bool expected = true;

Explication

2024 is divisible by 4 and not by 100.

Exemple 2
Entrée :
year = 2100

Sortie attendue :
expected = False
Entrée :
long long year = 2100;

Sortie attendue :
bool expected = false;
Entrée :
long year = 2100;

Sortie attendue :
bool expected = false;

Explication

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

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

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

Indice 2

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

Indice 3

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

Complexité attendue

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

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.