Dễ 0 đã giải Tỷ lệ chấp nhận 0%

Leap Year

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

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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.

Ràng buộc

1 ≤ year ≤ 9999.

Ví dụ

Ví dụ 1
Đầu vào:
year = 2024

Kết quả mong đợi:
expected = True
Đầu vào:
long long year = 2024;

Kết quả mong đợi:
bool expected = true;
Đầu vào:
long year = 2024;

Kết quả mong đợi:
bool expected = true;

Giải thích

2024 is divisible by 4 and not by 100.

Ví dụ 2
Đầu vào:
year = 2100

Kết quả mong đợi:
expected = False
Đầu vào:
long long year = 2100;

Kết quả mong đợi:
bool expected = false;
Đầu vào:
long year = 2100;

Kết quả mong đợi:
bool expected = false;

Giải thích

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

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

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

Gợi ý 2

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

Gợi ý 3

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

Độ 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.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.