当前位置:网站首页>实例004:这天第几天 输入某年某月某日,判断这一天是这一年的第几天?
实例004:这天第几天 输入某年某月某日,判断这一天是这一年的第几天?
2022-07-05 08:16:00 【懒笑翻】
实例004:这天第几天
题目:输入某年某月某日,判断这一天是这一年的第几天?
程序分析:计算这一天是该年的第几天,我们首先要计算出这个月之前那几个月总共有多少天,再加上该月的日期。前面几个月的天数求和中,除了2月份在年份为闰年的时候为29天,平年的时候为28天外,每个月的天数都是固定的,因此我们可以先设定一个月份天数的列表,当输入的年份是闰年时再把列表中2月的天数改为29天。
# 实例004:这天第几天
# **题目:**输入某年某月某日,判断这一天是这一年的第几天?
# 用于判断该年是否是闰年
def isLeapYear(y):
return (y % 400 == 0 or (y % 4 == 0 and y % 100 != 0))
# 定义1-11月每个月的天数
DofM = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30]
res = 0 #计算第几天
# 输入某年某月某日
year = int(input('Year:'))
month = int(input('Month:'))
day = int(input('day:'))
if isLeapYear(year): # 如果是闰年 则2月为29天
DofM[2] = 29
for i in range(month): # 计算month之前的总天数
res += DofM[i]
# 这一年的第几天 是 month之前的总天数+这个月的天数day
print(res + day)
结果如下:
边栏推荐
- Management and use of DokuWiki (supplementary)
- Hardware 3 -- function of voltage follower
- Measurement fitting based on Halcon learning [III] PM_ measure_ board. Hdev routine
- FIO测试硬盘性能参数和实例详细总结(附源码)
- Semiconductor devices (III) FET
- Various types of questions judged by prime numbers within 100 (C language)
- UEFI development learning 4 - getting to know variable services
- Bootloader implementation of PIC MCU
- More than 90% of hardware engineers will encounter problems when MOS tubes are burned out!
- Briefly talk about the identification protocol of mobile port -bc1.2
猜你喜欢
DCDC circuit - function of bootstrap capacitor
[tutorial 15 of trio basic from introduction to proficiency] trio free serial communication
Measurement fitting based on Halcon learning [i] fuse Hdev routine
【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
MySQL之MHA高可用集群
[paper reading] the latest transfer ability in deep learning: a survey in 2022
Brief discussion on Buck buck circuit
MySQL MHA high availability cluster
Let's briefly talk about the chips commonly used in mobile phones - OVP chips
Halcon's practice based on shape template matching [2]
随机推荐
MySQL之MHA高可用集群
Classic application of MOS transistor circuit design (1) -iic bidirectional level shift
QEMU STM32 vscode debugging environment configuration
Some tips for using source insight (solve the problem of selecting all)
STM32 tutorial triple ADC interleaved sampling
[trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code
动力电池UL2580测试项目包括哪些
Working principle and type selection of common mode inductor
Semiconductor devices (I) PN junction
UEFI development learning 4 - getting to know variable services
Keil use details -- magic wand
QEMU demo makefile analysis
【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
Connection mode - bridge and net
Soem EtherCAT source code analysis I (data type definition)
Why is 1900 not a leap year
[trio basic from introduction to mastery tutorial XIV] trio realizes unit axis multi-color code capture
Improve lighting C program
UE像素流,来颗“减肥药”吧!
Volatile of C language