簡単 7件解決済み 正答率36.8%

Days in a Month

Return how many days a month has, including leap years.

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

  • Map months to day counts
  • Reuse a leap-year rule
  • Split logic into small functions

問題

翻訳を表示

Return the number of days in month of year in the Gregorian calendar. February has 29 days in a leap year: years divisible by 4, except those divisible by 100 unless also divisible by 400. A helper function is_leap(year) keeps the code tidy.

Implement days_in_month(year, month).

Input

year: The year. month: The month, 1 (January) to 12 (December).

Output

Return the number of days.

制約

1 ≤ year ≤ 9,999; 1 ≤ month ≤ 12

例

例 1
入力:
year = 2024
month = 2

期待される出力:
expected = 29
入力:
long long year = 2024;
long long month = 2;

期待される出力:
long long expected = 29;
入力:
long year = 2024;
long month = 2;

期待される出力:
long expected = 29;

解説

2024 is divisible by 4 and not by 100, so it is a leap year: 29 days.

例 2
入力:
year = 1900
month = 2

期待される出力:
expected = 28
入力:
long long year = 1900;
long long month = 2;

期待される出力:
long long expected = 28;
入力:
long year = 1900;
long month = 2;

期待される出力:
long expected = 28;

解説

1900 is divisible by 100 but not 400, so it is not a leap year: 28 days.

解き方のヒント

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

ヒント 1

Most months have a fixed day count; February depends on whether the year is leap.

ヒント 2

Use a small lookup for month lengths, then adjust February with the leap-year rule.

ヒント 3

Be careful with century years: divisible by 100 is not enough unless also divisible by 400.

目安の計算量

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

参考資料

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

ランキング

全体ランキング

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

# コーダー 言語 時間 メモリ AI 評価 解答
1 Н"Никита "Programmer" Python 794 ms - 5/5 コードを見る
2 HJhettory Jurash Python 828 ms - 4/5 コードを見る
3 LMLaura Mahlow Python 843 ms - 4/5 コードを見る
4 ゼゼロダニ Python 845 ms - 4/5 コードを見る
5 KKKaneki Ken Python 866 ms - 4/5 コードを見る
6 ·𝓕·.★·.·⍣·.·★ 𝓕𝓪𝓫𝓲𝓸𝓵𝓪 𝓔𝓵𝓮𝓷𝓪 ★·.·⍣·.·★.· C++ 3,167 ms - 4/5 コードを見る
7 PTphúc thịnh phạm C++ 3,331 ms - 4/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

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