程序4--日期是一年的第几天
2017-05-09 17:27
375 查看
题目:输入某年某月某日,判断这一天是这一年的第几天?
程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于2时需考虑多加一天:
def number_of_days():
year = int(input("year:"))
month = int(input("month:"))
day = int(input("day:"))
months = [0,31,59,90,120,151,182,212,243,273,304,334]
num = 0
if 0<month<=12:
num = months[month-1]
else:
print("Data error")
num += day
if (year % 400 == 0)or((year % 4 == 0)and(year % 100 != 0)):
if month > 2:
num +=1
print('it is the %dth day.' % num)
运行:
>>> number_of_days()
year:2024
month:5
day:9
it is the 130th day.
另外一些方法:
import time
def number_of_days2():
a = input("输入时间(格式如:2017-04-04):")
t = time.strptime(a,"%Y-%m-%d")
print(time.strftime("今年的第%j天",t))
另外:
>>> p = [31,28,31,30,31,30,31,31,30,31,30,31]
>>> sum(p[0:2])
59
可以求数组的p的某一段的和。
程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于2时需考虑多加一天:
def number_of_days():
year = int(input("year:"))
month = int(input("month:"))
day = int(input("day:"))
months = [0,31,59,90,120,151,182,212,243,273,304,334]
num = 0
if 0<month<=12:
num = months[month-1]
else:
print("Data error")
num += day
if (year % 400 == 0)or((year % 4 == 0)and(year % 100 != 0)):
if month > 2:
num +=1
print('it is the %dth day.' % num)
运行:
>>> number_of_days()
year:2024
month:5
day:9
it is the 130th day.
另外一些方法:
import time
def number_of_days2():
a = input("输入时间(格式如:2017-04-04):")
t = time.strptime(a,"%Y-%m-%d")
print(time.strftime("今年的第%j天",t))
另外:
>>> p = [31,28,31,30,31,30,31,31,30,31,30,31]
>>> sum(p[0:2])
59
可以求数组的p的某一段的和。
相关文章推荐
- C#8—写一个测试标准身高体重的程序、输入日期年月日, 计算这一天是一年中的第几天(考虑闰年)
- 黑马程序员---------------------编写程序,该程序启动后用户可以按“yyyy-MM-dd”的格式输入一个日期, * 程序计算这一天是星期几,并且计算出是一年中的第几天
- 【小工具】日期与一年中第几天的转换程序
- 黑马程序员 测试题:编写程序,该程序启动后用户可以按“yyyy-MM-dd”的格式输入一个日期,程序计算这一天是星期几,并且计算出是一年中的第几天
- 编写程序,该程序启动后用户可以按“yyyy-MM-dd”的格式输入一个日期,程序计算这一天是星期几,并且计算出是一年中的第几天
- 黑马程序员—编写程序,该程序启动后用户可以按“yyyy-MM-dd”的格式输入一个日期, *程序计算这一天是星期几,并且计算出是一年中的第几天。 */
- 用C编程计算某日期是一年中第几天
- 【程序4】 题目:输入某年某月某日,判断这一天是这一年的第几天?
- 计算日期是一年中的第几天(不用函数)
- 用C++实现,输入一个日期,输出它是一年中的第几天。
- 编写程序判断某年某月某日这一年中是第几天。
- 程序3(完全平方数) and 程序4(一年的第几天)
- C语言根据日期取其位于一年中的第几天
- 【程序3】题目:输入某年某月某日,判断这一天是这一年的第几天?
- 编写程序计算所输日期是当年的第几天
- 【程序14】 TestDay.java 题目:输入某年某月某日,判断这一天是这一年的第几天?
- C#--第2周实验--任务9--编写一个控制台应用--输入一个日期,求该日期是这一年中的第几天
- 用C语言来实现_输入一个日期(年月日),计算是这一年中的第几天
- 计算日期是一年中的第几天
- Java 获得指定日期是一年中的第几天