def is_leap_year(year: int) -> bool:
"""
判断年份是否是闰年
闰年的2月有29天,平年的2月只有28天。
算法思想:
(1) 判断是否是世纪年。
(2) 若是世纪年,判断能否被400整除。若可以被整除,则是闰年;否则是平年。
(3) 若是平年,判断能否被4整除。若可以被整除,则是闰年;否则是平年。
:param year: 年份
:return: 闰年返回True,平年返回False
"""
if not year % 100:
if year % 400:
return False
else:
return True
else:
if year % 4:
return False
else:
return True
if __name__ == '__main__':
result = is_leap_year(1900)
print(result)
result = is_leap_year(2000)
print(result)
result = is_leap_year(2010)
print(result)
result = is_leap_year(2016)
print(result)
"""
运行结果:
False
True
False
True
Process finished with exit code 0
"""