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

Counting Sundays

Count the months in a range of years that begin on a Sunday.

สิ่งที่คุณจะได้เรียนรู้

  • Apply the Gregorian leap year rule
  • Track the weekday as days pass
  • Count events over a range of months

โจทย์

ดูฉบับแปล

A club meets on the first day of every month, and those meetings that land on a Sunday get a brunch. Count the months from January of start_year to December of end_year whose first day is a Sunday, using the Gregorian calendar. As a fixed point: 1 January 2023 was a Sunday. Remember the leap year rule: divisible by 4, except century years, which must be divisible by 400.

Implement counting_sundays(start_year, end_year).

Input

start_year: The first year, counted from January.

end_year: The last year, counted through December.

Output

Return the number of months in that range whose first day is a Sunday.

เงื่อนไข

1600 ≤ start_year ≤ end_year ≤ 2400

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
start_year = 2023
end_year = 2023

ผลลัพธ์ที่คาดหวัง:
expected = 2
อินพุต:
long long start_year = 2023;
long long end_year = 2023;

ผลลัพธ์ที่คาดหวัง:
long long expected = 2;
อินพุต:
long start_year = 2023;
long end_year = 2023;

ผลลัพธ์ที่คาดหวัง:
long expected = 2;

คำอธิบาย

In 2023 only January and October began on a Sunday, so the answer is 2.

ตัวอย่างที่ 2
อินพุต:
start_year = 2024
end_year = 2025

ผลลัพธ์ที่คาดหวัง:
expected = 3
อินพุต:
long long start_year = 2024;
long long end_year = 2025;

ผลลัพธ์ที่คาดหวัง:
long long expected = 3;
อินพุต:
long start_year = 2024;
long end_year = 2025;

ผลลัพธ์ที่คาดหวัง:
long expected = 3;

คำอธิบาย

2024 had September and December, 2025 had June: 3 months.

แนวทางการแก้

ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ

คำใบ้ 1

Find the weekday of January 1 of the start year from a known date, or use a weekday formula.

คำใบ้ 2

Moving to the next month shifts the weekday by that month's length mod 7.

คำใบ้ 3

February has 29 days in leap years: divisible by 4, but century years only when divisible by 400.

ความซับซ้อนที่คาดหวัง

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

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

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

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

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