簡単 2件解決済み 正答率66.7%

Leap Year

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

このチャレンジで学べること

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

問題

翻訳を表示

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.

制約

1 ≤ year ≤ 9999.

例

例 1
入力:
year = 2024

期待される出力:
expected = True
入力:
long long year = 2024;

期待される出力:
bool expected = true;
入力:
long year = 2024;

期待される出力:
bool expected = true;

解説

2024 is divisible by 4 and not by 100.

例 2
入力:
year = 2100

期待される出力:
expected = False
入力:
long long year = 2100;

期待される出力:
bool expected = false;
入力:
long year = 2100;

期待される出力:
bool expected = false;

解説

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

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

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

ヒント 2

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

ヒント 3

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

目安の計算量

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

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

# コーダー 言語 時間 メモリ AI 評価 解答
1 KKKaneki Ken Python 1,006 ms - 5/5 コードを見る
2 GTGarlic The Destroyer Rust 1,455 ms - 5/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

ネタバレ注意:下に2件の全解答あり まずは自分で挑戦してみましょう。他の開発者のコードやAIレビューも見ることができます。 サインインして課題を解くと議論に参加できます。