ง่าย 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

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

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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก