1

## Python 閏年計算問題

year = eval(input())
if year % 4 ==0 and (year % 100 !=0 or year % 400==0):
print('It is a leap year')
else:
print('It is not a leap year')

https://zh.wikipedia.org/wiki/%E9%97%B0%E5%B9%B4

### 5 個回答

4
Darwin Watterson
iT邦新手 1 級 ‧ 2019-11-13 23:05:28

`能被 4 整除 但不能被 100 整除`

`再考慮其他的能不能被 400 整除`

``````year % 4 ==0 and (year % 100 !=0 or year % 400==0)
``````

`不能被 4 整除的`

`能被 4 整除的卻不被 100 整除`

`能被 4 整除也能被 400 整除`

(1801-2400間的閏年似乎都能被4整除)

dragonH iT邦大師 1 級 ‧ 2019-11-13 23:23:41 檢舉

3
paicheng0111
iT邦高手 2 級 ‧ 2019-11-13 23:05:33

``````year = int(input())
if (year % 4 ==0 and year % 100 !=0) or year % 400==0:
print('It is a leap year')
else:
print('It is not a leap year')
``````

``````a = int(input())
if (a%4==0 and a%100!=0) or (a%400==0):
print("LEAP")
else:
print("COMMON")
``````

3

iT邦大師 1 級 ‧ 2019-11-14 01:20:32

``````if year % 4 ==0 and (year % 100 !=0 or year % 400==0):
``````

``````y%400==0 or (y%4==0 and y%100!=0)
``````

``````year%4 ==0 and (year % 100 !=0 or year % 400==0)
``````

``````year = int(input())

if year%400==0:
print("leap")
elif year%4 ==0 and year%100>0:
print("leap")
else:
print("normal")
``````
0

iT邦好手 1 級 ‧ 2019-11-14 06:22:00

``````if(){
if(){
if()
...
}

}
``````

sion iT邦新手 4 級 ‧ 2019-11-14 09:31:54 檢舉

``````if(){
if(){
if()
...
}
}
``````

1

iT邦高手 1 級 ‧ 2019-11-14 11:46:16

https://ithelp.ithome.com.tw/articles/10214650

``````def isLeapYear(n):
year = int(n)
return (year % 400 == 0) or ((year % 100 != 0) and (year % 4 == 0))

ylist = [2000, 1999, 2001, 2004, 1900]

xlist = [(x, isLeapYear(x)) for x in ylist]

import calendar as cal
xlist2 = [(x, cal.isleap(x)) for x in ylist]

>>> print(xlist)
[(2000, True), (1999, False), (2001, False), (2004, True), (1900, False)]

>>> print(xlist2)
[(2000, True), (1999, False), (2001, False), (2004, True), (1900, False)]
``````