题解 | #计算日期到天数转换#
计算日期到天数转换
http://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded
def isleap_year(n):##定义一个是否是闰年的函数
if n % 4 == 0 and n % 100 != 0:##闰年的第一种情况:2004、1904...但1900不是
return True
elif n % 400 == 0:##闰年的第二种情况:2000、1600...
return True
else:
return False
i = input()
rq = list(map(int, i.split( )))######注意map(int, i.split( ))的类型为<class 'map'>,所以要用list转化
nian=rq[0]
yue=rq[1]
ri=rq[2]
none_leap_dic={1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
leap_dic={1:31,2:29,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
t=0
if isleap_year(rq[0]):
for y in range(1,rq[1]):
t=t+leap_dic.get(y)
t=t+rq[2]
else:
for y in range(1,rq[1]):
t=t+none_leap_dic.get(y)
t=t+rq[2]
print(t)
if n % 4 == 0 and n % 100 != 0:##闰年的第一种情况:2004、1904...但1900不是
return True
elif n % 400 == 0:##闰年的第二种情况:2000、1600...
return True
else:
return False
i = input()
rq = list(map(int, i.split( )))######注意map(int, i.split( ))的类型为<class 'map'>,所以要用list转化
nian=rq[0]
yue=rq[1]
ri=rq[2]
none_leap_dic={1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
leap_dic={1:31,2:29,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
t=0
if isleap_year(rq[0]):
for y in range(1,rq[1]):
t=t+leap_dic.get(y)
t=t+rq[2]
else:
for y in range(1,rq[1]):
t=t+none_leap_dic.get(y)
t=t+rq[2]
print(t)
【牛客站内】华为机试题—简单 文章被收录于专栏
【牛客站内】华为机试题练习记录