题目:输入某年某月某日,判断这一天是这一年的第几天?
程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于2时需考虑多加一天:
year = int(input('year:'))
month = int(input('month:'))
day = int(input('date:'))
a = [0,31,59,90,120,151,181,212,243,273,304,334]
b = [0,31,60,91,121,152,182,213,244,274,305,335]
if year % 400 ==0 or (year % 4 ==0 and year %100 != 0):
number = b[month-1] + day
print(number)
else:
number = a[month -1] + day
print(number)
运行结果:
year:2015
month:6
date:7
158