每天一题:输出第几天 by python(19.9.10)

题目描述:

输入一个“YYYY-MM-dd”格式的日期字符串,输出这一天是当年的第几天(1月1日是每年的第一天)

输入描述:一个“YYYY-MM-dd”格式的表示日期的字符串

输出描述:这一天是当年的第几天

示例:输入: 2019-01-09 输出:9

          输入:2004-03-01 输出:61

思路:首先判断是不是闰年;再根据闰年做相应的处理

代码:

def is_leap_year(year):
    if (year % 4 == 0 and year %100 != 0) or year % 400 ==0:
        return True
    else:
        return False
def count_day(year, month, day):
    leap_year = [31,29,31,30,31,30,31,31,30,31,30,31]
    not_leap_year = [31,28,31,30,31,30,31,31,30,31,30,31]
    count =0
    if is_leap_year(year):
        count = sum(leap_year[:month - 1]) + day
    else:
        count = sum(not_leap_year[:month - 1]) + day
    return count

a = [int(x) for x in input().split('-')]
year = a[0]
month = a[1]
day = a[2]
print(count_day(year, month, day))

 

你可能感兴趣的:(每日一题)