一年的第几天
对闰年的设定根据possible变量来递增,其它则分别进行累加即可。
def the_nth_days(seq):
if not seq:
return 0
months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
values = {du: mu for du, mu in zip(range(1, 13), months)}
possible = 0
year, month, day = seq[0], seq[1], seq[2]
if (year % 4 == 0 or year % 100 == 0) and (year % 100 != 0):
possible = 1
res = 0
for mu in range(1, month):
res += values[mu]
if mu == 2:
res += possible
res += day
return res
if __name__ == '__main__':
seq = list(map(int, input().strip().split()))
res = the_nth_days(seq)
print(res)
'''
2019 2 2
'''
(最近更新:2019年09月16日)
本文介绍了一个Python函数,用于计算给定日期是一年中的第几天。通过考虑月份天数和是否为闰年,该函数能够准确计算出日期位置。特别地,文章详细解释了如何判断闰年,并在此基础上调整二月的天数。
948

被折叠的 条评论
为什么被折叠?



