蓝桥杯练习题--一年中的第几天
1. 方法一:for循环
解题思路:
- 自定义两个列表,分别存放平年与闰年每月对应的天数
- 判断是闰年还是平年(若可以被4整除且不能被100整除为闰年,或者可以被400整除为闰年)
- for循环遍历,使月份对应的天数累加,再加上天数即可得出答案
ping=[31,28,31,30,31,30,31,31,30,31,30,31]
run=[31,29,31,30,31,30,31,31,30,31,30,31]
while True:
y,m,d=list(map(int,input().split()))
if y==0 and m==0 and d==0:
break
if y%4==0 and y%100!=0 or y%400==0:
# 判断是闰年
res=0
for i in range(m-1):
res+=run[i]
res+=d
print(res)
else:
res=0
for j in range(m-1):
res+=ping[j]
res+=d
print(res)
2. 方法二:导入date
date()可以将输入的年月日转换为时间类型,并进行相减计算(方便简洁)
from datetime import date
while True:
a, b, c = map(int, input().split())
if a==0 and b==0 and c==0:
break
d1 = date(a, b, c)
d2 = date(a, 1, 1)
print((d1 - d2).days + 1)