题解 | 计算一年中的第几天
计算一年中的第几天
https://www.nowcoder.com/practice/178aa3dafb144bb8b0445edb5e9b812a
import sys def is_leap_year(year): """判断是否为闰年""" return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0) def day_of_year(year, month, day): """计算某天是该年的第几天""" # 每月的天数,2月暂按28天处理(后面根据闰年调整) month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] if is_leap_year(year): month_days[1] = 29 # 闰年2月有29天 total_days = 0 for m in range(month - 1): # 累加前 month-1 个月的天数 total_days += month_days[m] total_days += day # 加上当前月的天数 return total_days # 处理多组输入 while True: try: Y, M, D = map(int, input().split()) print(day_of_year(Y, M, D)) except: break