题解 | #计算日期到天数转换#
计算日期到天数转换
http://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded
简单但代码写起来稍显复杂的方式就是将每个月对应的天数放入字典:
while True:
try:
year, month, day = map(int, input().split())
days = 0
# 判断是否闰年,闰年2月29天,平年2月28天
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
days_of_months = {1: 31, 2: 29, 3: 31, 4: 30, 5: 31,
6: 30, 7: 31, 8: 31, 9: 30, 10: 31,
11: 30, 12: 31
}
else:
days_of_months = {1: 31, 2: 28, 3: 31, 4: 30, 5: 31,
6: 30, 7: 31, 8: 31, 9: 30, 10: 31,
11: 30, 12: 31
}
# 从1月开始,键值相加
for i in range(1, month): # 此处不包含输入月份的天数,因为输入的月份的天数已经给出日期
days += days_of_months[i]
days += day # 加上输入的日期
print(days)
except:
break