1、问题描述:
思路:
代码:
def isLeapYear(n):
if (n%4==0 and n%100!=0) or n%400==0:#两个判断是否是闰年的条件必须是或的关系,不能缺少。
return True
else:
return False
def daysyear(date):
monthdy=[0,31,28,31,30,31,30,31,31,30,31,30,31]
if isLeapYear(date[0]):
monthdy[2]=29
smondys=sum(monthdy[0:date[1]])
days=smondys+date[2]
return days
if __name__=='__main__':
sumday=0
ymd=[]
ymd=[int(i)for i in raw_input().split('-')]
for i in range(2015,ymd[0]):
if not isLeapYear(i):
sumday=sumday+365
else:
sumday=sumday+366
sumday+=daysyear(ymd)-daysyear([2015,10,18])
print sumday
2、知识点补充:
2.1、 闰年是公历中的名词。普通年(不能被100整除的年份) 能被4整除的为闰年。(如2004年就是闰年,1999年不是闰年);
世纪年(能被100整除的年份) 能被400整除的是闰年。(如2000年是闰年,1900年不是闰年);
闰年(Leap Year)是为了弥补因人为历法规定造成的年度天数与地球实际公转周期的时间差而设立的。补上时间差的年份为闰年。 闰年共有366天(1-12月分别为31天,29天,31天,30天,31天,30天,31天,31天,30天,31天,30天,31天)。
2.2、公历中只分闰年和平年,平年有365天,而闰年有366天(2月中多一天);
2.3、
ymd=[int(i)for i in raw_input().split('-')]
等价于:
future_day = list(map(int,raw_input().split('-')))
其中map的知识点是:
Python map() 函数:
描述
map() 会根据提供的函数对指定序列做映射。
第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。
语法
map() 函数语法:
map(function, iterable, ...)
参数
function -- 函数,有两个参数
iterable -- 一个或多个序列
返回值
Python 2.x 返回列表。
Python 3.x 返回迭代器。
实例:
>>>def square(x) : # 计算平方数
... return x ** 2
...
>>> map(square, [1,2,3,4,5]) # 计算列表各个元素的平方
[1, 4, 9, 16, 25]
>>> map(lambda x: x ** 2, [1, 2, 3, 4, 5]) # 使用 lambda 匿名函数
[1, 4, 9, 16, 25]
# 提供了两个列表,对相同位置的列表数据进行相加
>>> map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
[3, 7, 11, 15, 19]
3、别人家的代码:
Python3 的:
def isLeapYear(year):
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
return True
else:
return False
def dayInYear(day):
month = [0,31,28,31,30,31,30,31,31,30,31,30,31]
sumday = day[2]
if isLeapYear(day[0]):
month[2] = 29
for i in range(1,day[1]):
sumday += month[i]
return sumday
count_day = 0
future_day = list(map(int,raw_input().split('-')))
for i in range(2015,future_day[0]):
if isLeapYear(i):
count_day += 366
else:
count_day += 365
count_day += dayInYear(future_day)-dayInYear([2015,10,18])
print(count_day)