一、閏年的計算方法
- 如果是世紀年,如1900,2000等可以被100整除的年份,只有可以被400整除的年份是閏年,其他年份是平年;
- 其他不是世紀年的年份可以被4整除的就是閏年,否則就是平年。
二、使用python來實現計算
# -*- coding: UTF-8 -*-
year = int(input("輸入一個年份: "))
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print("{0} 是閏年".format(year)) # 整百年能被400整除的是閏年
else:
print("{0} 不是閏年".format(year))
else:
print("{0} 是閏年".format(year)) # 非整百年能被4整除的爲閏年
else:
print("{0} 不是閏年".format(year))
如果要計算今年是平年還是閏年,只需要import time
import time
year_num = time.strftime('%Y',time.localtime(time.time()))
if is_leap_year(year_num):
print 'This year is a leap year!'
else:
print 'This year is not a leap year!'