모든 챌린지
보통 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에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

아직 통과한 풀이가 없습니다. 풀어서 1위를 차지하세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.