题目:输入某年某月某日,判断这一天是这一年的第几天?
分析:方法一,可以都按照每月31天来计算,然后减去相应的差值。
方法二,直接算好每个月1号需要加的天数,直接用这个数加上日期就行。
# -*- coding: utf-8 -*-
iptstr = input('请输入XXXX年XX月XX日:').split('/') #格式取2020/3/16
year = eval(iptstr[0])
month = eval(iptstr[1])
day = eval(iptstr[2])
dayth = (month - 1) * 31 + day
if month > 2:
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
dayth -= 1
else:
dayth -=2
if month < 9:
dayth -= (month - 1) // 2
else:
dayth -= (month - 2) //2
print('/'.join(iptstr) + '是一年中的第%d天'%dayth)
'''看了网上的解法后,那个更简单,是提前算好一部分放list里面的。具体整理入下'''
iptstr = input('请输入XXXX年XX月XX日:').split('/') #格式取2020/3/16
year = eval(iptstr[0])
month = eval(iptstr[1])
day = eval(iptstr[2])
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
month_days = [0,31,60,91,121,152,182,213,244,274,305,335]
else:
month_days = [0,31,59,90,120,151,181,212,243,273,304,334]
dayth = month_days[month - 1] + day
print('/'.join(iptstr) + '是一年中的第%d天'%dayth)