题解 | #计算日期到天数转换#

计算日期到天数转换

http://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded

def leap_year(year):
    if (year % 4) == 0:
       if (year % 100) == 0:
           if (year % 400) == 0:
               return 1
           else:
               return 0
       else:
           return 1
    else:
       return 0

while True:
    try:
        date_list=input().split()
        count_days=0
        dic_leap={1:31,2:29,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
        dic_nonleap={1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
        if leap_year(int(date_list[0]))==1:
            for i in range(1,int(date_list[1])):
                     count_days+=dic_leap.get(i)
            count_days+=int(date_list[2])
        else:            
            for i in range(1,int(date_list[1])):
                     count_days+=dic_nonleap.get(i)
            count_days+=int(date_list[2])
        print(count_days)
    except:
        break

全部评论
题目要求空间复杂度o(1),这代码用到预先定义好的字典,讲道理不符合要求吧
点赞 回复 分享
发布于 2022-02-23 23:23

相关推荐

我见java多妩媚:大外包
点赞 评论 收藏
分享
评论
9
1
分享
牛客网
牛客企业服务