第三天
闰年问题升级版
输入年月日,输出该日期是否是闰年,并且输出该日期是此年份的第几天
闰年判断条件:
1、能被 4 整除,并且不能被 100 整除。
2、能被 400 整除。
3、两个条件满足任意一个就为闰年。
算法思路:
1、接收用户输入的年月日,创建保存 12 个月份天数的列表。
2、根据年份判断是否是闰年,如果是把二月份设为 29 天,否则把二月份设为 28 天。
3、根据月份和日期统计是当年的第几天。
year = int(input('请输入年:'))
month = int(input('请输入月:'))
day = int(input('请输入日:'))
# 每月天数列表,索引0表示1月
days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
# 计算总天数
count_day = day
# 判断闰年并调整2月天数
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(f'{year}是闰年')
days_in_month[1] = 29
else:
print(f'{year}不是闰年')
# 累加当前月份之前的所有月份天数
for i in range(month - 1):
count_day += days_in_month[i]
print(f'{year}年{month}月{day}日是当年的第{count_day}天')