你的算法bug是如果1号是周六就不会计2公里,具体位置看注释li_run = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
li_ping = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
# 每年多少天
di_n = {}
for i in range(2000,2021):
if i % 4 == 0 and i % 100 != 0:
di_n[i] = li_run
elif i % 400 == 0:
di_n[i] = li_run
else:
di_n[i] = li_ping
di_n[2020] = di_n[2020][:9]
# 2000.1.1周6
week = [1,1,2,1,1,1,1]
# 存每天跑多少km
di_w = {}
km = 0
year = 2000
n = 0
while year != 2021:
for m_day in di_n[year]:
for day in range(1,m_day+1):
if n < 7:
if day == 1:
di_w[day] = 2
n += 1
else:
di_w[day] = week[n]
n += 1
else:
n = 0
if day == 1: #这是周六时候执行的代码,如果不加判断,无论是不是1号都+1
di_w[day] = 2
n += 1
else:
di_w[day] = week[n]
n += 1
km += sum(di_w.values())
if year == 2020:
print(di_w)
print(km)
di_w.clear()
year += 1
print(km+2)
# 8879
。
|