簡単 0件解決済み 正答率0%

Days in a Month

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

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

  • 月を日数に対応付ける
  • うるう年の規則を再利用する
  • 処理を小さな関数に分ける

問題

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

多くの月は日数が固定ですが、2月はうるう年かどうかで変わります。

ヒント 2

月ごとの日数は小さな表で管理し、2月だけうるう年判定で調整します。

ヒント 3

100で割れるだけでは不十分で、400でも割れる年だけ例外になることに注意してください。

目安の計算量

時間 O(1)、追加メモリ O(1)

参考資料

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

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。