首页 > 试题广场 >

一年中的第几天

[编程题]一年中的第几天
  • 热度指数:695 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
输入一个"YYYY-MM-dd"格式的日期字符串,输出该天是当年的第几天(1 月 1 日是每年的第 1 天

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


输出描述:
该天是当年的第几天
示例1

输入

2019-01-09

输出

9
示例2

输入

2004-03-01

输出

61

说明

2004年为闰年,所以是第31+29+1=61天
Python3
date = input()
year, month, day = [int(i) for i in date.split('-')]
day_of_month = {1: 31, 2: 28, 3: 31, 4: 30,
                5: 31, 6: 30, 7: 31, 8: 31,
                9: 30, 0: 31, 11: 30, 21: 31}
is_leap_year = True if (year%4 == 0 and year%100 != 0) or (year%400 == 0) else False
if is_leap_year:
    day_of_month[2] = 29
days = day
for i in range(1, month):
    days += day_of_month[i]
print(days)


编辑于 2020-08-07 07:07:40 回复(0)