Đề bài
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.
Ràng buộc
1 ≤ year ≤ 9,999; 1 ≤ month ≤ 12
Ví dụ
Đầu vào:
year = 2024
month = 2
Kết quả mong đợi:
expected = 29
Đầu vào:
long long year = 2024;
long long month = 2;
Kết quả mong đợi:
long long expected = 29;
Đầu vào:
long year = 2024;
long month = 2;
Kết quả mong đợi:
long expected = 29;
Giải thích
2024 is divisible by 4 and not by 100, so it is a leap year: 29 days.
Đầu vào:
year = 1900
month = 2
Kết quả mong đợi:
expected = 28
Đầu vào:
long long year = 1900;
long long month = 2;
Kết quả mong đợi:
long long expected = 28;
Đầu vào:
long year = 1900;
long month = 2;
Kết quả mong đợi:
long expected = 28;
Giải thích
1900 is divisible by 100 but not 400, so it is not a leap year: 28 days.
Hướng tiếp cận
Hãy tự thử trước, sau đó mở từng gợi ý một.
Gợi ý 1
Most months have a fixed day count; February depends on whether the year is leap.
Gợi ý 2
Use a small lookup for month lengths, then adjust February with the leap-year rule.
Gợi ý 3
Be careful with century years: divisible by 100 is not enough unless also divisible by 400.
Độ phức tạp mong đợi
O(1) time, O(1) extra space
Tham khảo
Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.