Einfach 0 gelöst 0% Erfolgsquote

Leap Year

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

Das lernst du

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

Problem

Übersetzung anzeigen

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.

Einschränkungen

1 ≤ year ≤ 9999.

Beispiele

Beispiel 1
Eingabe:
year = 2024

Erwartete Ausgabe:
expected = True
Eingabe:
long long year = 2024;

Erwartete Ausgabe:
bool expected = true;
Eingabe:
long year = 2024;

Erwartete Ausgabe:
bool expected = true;

Erläuterung

2024 is divisible by 4 and not by 100.

Beispiel 2
Eingabe:
year = 2100

Erwartete Ausgabe:
expected = False
Eingabe:
long long year = 2100;

Erwartete Ausgabe:
bool expected = false;
Eingabe:
long year = 2100;

Erwartete Ausgabe:
bool expected = false;

Erläuterung

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

So gehst du vor

Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.

Hinweis 1

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

Hinweis 2

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

Hinweis 3

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

Erwartete Komplexität

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

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.