咪咕:笔试题(20190916)

本文介绍了一个Python函数,用于计算给定日期是一年中的第几天。通过考虑月份天数和是否为闰年,该函数能够准确计算出日期位置。特别地,文章详细解释了如何判断闰年,并在此基础上调整二月的天数。

一年的第几天

对闰年的设定根据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日)

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值