Write a Functon
def is_leap(year):
leap = False
leap = (year%400==0) or (year%100!=0 and year%4==0)
return leap
year = int(input())
print(is_leap(year))
해설
윤년을 구분하는 프로그래밍이며, 알고리즘은 아래와 같다.
ㅇ 4로 나누어 떨어지고
ㅇ 100으로 나누어 떨어지나,
ㅇ 400으로 나누어 떨어지면 '윤년'
# 2000년은 윤년이다.
>>> 2000 % 4 == 0
True
>>> 2000 % 100 == 0
True
>>> 2000 % 400 == 0
True
# 2100년은 윤년이 아니다.
>>> 2100 % 4 == 0
True
>>> 2100 % 100 == 0
True
>>> 2100 % 400 == 0
False
# 2020년은 윤년이다.
>>> 2020 % 4 == 0
True
>>> 2020 % 100 == 0
False
>>> 2020 % 400 == 0
False
'Develop > hackerrank.com' 카테고리의 다른 글
HackerRank Python Solution / List Comprehensions (0) | 2022.01.01 |
---|---|
HackerRank Python Solution / Print Function (0) | 2022.01.01 |
HackerRank Python Solution / Loops (0) | 2021.12.30 |
HackerRank Python Solution / Division (0) | 2021.11.12 |
HackerRank Python Solution / Arithmetic Operators (0) | 2021.11.12 |