본문 바로가기

파이썬

[Project Euler]19. Counting Sundays

[문제]
You are given the following information, but you may prefer to do some research for yourself.

  • 1 Jan 1900 was a Monday.
  • Thirty days has September,
    April, June and November.
    All the rest have thirty-one,
    Saving February alone,
    Which has twenty-eight, rain or shine.
    And on leap years, twenty-nine.
  • A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.

How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?

 

[풀이]
import calendar

sunCount=0
for yr in range(1901,2001):
    for mon in range(1,13):
        a= calendar.weekday(yr,mon,1)
        if a==6:
            sunCount+=1
print(sunCount)

'''
예전에는 어렵게 풀어야했던 문제 같은데...
calendar를 사용해서 쉽게 풀었다.

너무 오랜만에 문제를 풀어서 그런가 머리가 딱딱하게 굳은 느낌....
다시 힘내서 열심히 풀어보기로 다짐한다!!
'''

'파이썬' 카테고리의 다른 글

[Project Euler]21. Amicable numbers  (0) 2021.07.11
[Project Euler]20. Factorial digit sum  (0) 2021.07.06
[Project Euler]18. 못풀었음  (0) 2021.06.29
[Project Euler]17. Number letter counts  (0) 2021.06.10
[Project Euler]16. Power digit sum  (0) 2021.06.10