、给定某年某月某日判断这一天是这一年的第几天? 程序分析:以3月5日为例应该先把前两个月的加起来然后再加上5天即本年的第几天特殊情况闰年且输入月份大于3时需考虑多加一天。 闰年的条件:year除以400能整除或者year除以4能整除但是不能是100的倍数。 根据月份记录总天数1 3 5 7 8 10 12 314 6 9 11 302 29闰年 28平年 返回值类型:int参数列表:int
def is_leap_year(year):
if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
return True
else:
return False
def day_of_year(year, month, day):
days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if is_leap_year(year):
days_in_month[1] = 29
total_days = 0
for i in range(month - 1):
total_days += days_in_month[i]
total_days += day
return total_days
year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
day = int(input("请输入日期:"))
result = day_of_year(year, month, day)
print("{}年{}月{}日是这一年的第{}天".format(year, month, day, result))
示例输出: 请输入年份:2022 请输入月份:3 请输入日期:5 2022年3月5日是这一年的第64
原文地址: https://www.cveoy.top/t/topic/hX0n 著作权归作者所有。请勿转载和采集!