2、3、5过不了,python

P5737 【深基7.例3】闰年展示

loren111 @ 2023-03-02 09:03:50

n = list(map(int,input().split()))
def rn(n):
  if ((n % 400 == 0) or ((n % 4 == 0) and (n // 100 != 0))):
    return 1
  else:
    return 0

s = []
for i in range(n[0],n[1]+1):
  if rn(i):
    s.append(i)
print(len(s))
for j in s:
  print(j,end=' ')

by Math_Cai @ 2023-11-16 13:41:49

你闰年判断部分有误哦, 这行有问题

if ((n % 400 == 0) or ((n % 4 == 0) and (n // 100 != 0))):

应该改为

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

即:将 ‘//’ 改为 ‘%’

感觉你这样写麻烦了,我的ac代码如下(有不足欢迎补充、指正)

x,y=input().split()
x=int(x)
y=int(y)
n=0
A=[]
for c in range(x,y+1,+1):
    if (c%4==0 and c%100!=0) or c%400==0:
        n+=1
        A.append(c)
print(n)
for i in A:
    print(i,end=' ')

|