Python 练习实例4
题目:输入某年某月某日,判断这一天是这一年的第几天?
程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于2时需考虑多加一天:
程序源代码:
实例(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
year = int(raw_input('year:\n'))
month = int(raw_input('month:\n'))
day = int(raw_input('day:\n'))
months = (0,31,59,90,120,151,181,212,243,273,304,334)
if 0 < month <= 12:
sum = months[month - 1]
else:
print 'data error'
sum += day
leap = 0
if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
leap = 1
if (leap == 1) and (month > 2):
sum += 1
print 'it is the %dth day.' % sum
以上实例输出结果为:
year: 2015 month: 6 day: 7 it is the 158th day.
Python 100例
ym
853642877@qq.com
参考解法:
#!/usr/bin/python # -*- coding: UTF-8 -*- year=int(raw_input("年:\n")) month=int(raw_input("月:\n")) day=int(raw_input("日:\n")) months1=[0,31,60,91,121,152,182,213,244,274,305,335,366] #闰年 months2=[0,31,59,90,120,151,181,212,243,273,304,334,365] #平年 if ((year%4==0)and(year%100!=0)) or((year%100==0)and(year%400==0)): Dth=months1[month-1]+day else: Dth=months2[month-1]+day print "是该年的第%d天"%Dthym
853642877@qq.com
苹果pai
646999039@qq.com
闰年需要同时满足以下条件:
#!/usr/bin/python # -*- coding: UTF-8 -*- # 输入任意年月日,知道是改年第几天 p = [31,28,31,30,31,30,31,31,30,31,30,31] # 平年 w = [31,29,31,30,31,30,31,31,30,31,30,31] # 闰年 year =int(raw_input("请输入年:"+'\n')) month =int(raw_input("请输入月:"+'\n')) day=int(raw_input("请输入日:"+'\n')) arr=[31,28,31,30,31,30,31,31,30,31,30,31] sum=day for i in range(0,month-1): sum+=arr[i] if year%4==0: if year%100==0 and year%400!=0: print "这是今年的第%d天" % sum else: sum=sum+1 print "这是今年的第%d天" % sum else: print "这是今年的第%d天" % sum苹果pai
646999039@qq.com
流年细雨
758896823@qq.com
参考解法:
#!/usr/bin/python # -*- coding: UTF-8 -*- # 输入任意年月日,知道是改年第几天 p = [31,28,31,30,31,30,31,31,30,31,30,31] # 平年 w = [31,29,31,30,31,30,31,31,30,31,30,31] # 闰年 year =int(raw_input("年:\n")) month =int(raw_input("月:\n")) day =int(raw_input("日:\n")) # 判断闰年,平年 if year % 100 == 0: if year % 400 == 0: d=w else: d=p else: if year % 4 == 0: d=w else: d=p # 计算天数 days = sum(d[0:month-1])+day print "%d.%d.%d是%d年的第%s天。"%(year,month,day,year,days)流年细雨
758896823@qq.com
Atom
tumucn@126.com
参考解法:
#!/usr/bin/python # -*- coding: UTF-8 -*- year = int(input('请输入年份:')) month = int(input('请输入月份:')) day = int(input('请输入日期:')) total = 0 if year%4 == 0: days = 29 else: days = 28 if year%4 == 0: print year, '年是润年,2月有', days, '天!' else: print year, '年是平年,2月有', days, '天!' if month <= 1: total = day elif month == 2: total = 31 + day else: total = (month - 2) * 30 + days + month/2 + day print year, '年',month, '月', day, '日,是这一年的第', total, '天!'Atom
tumucn@126.com
老虎头
shali1026@163.com
参考解法:
#!/usr/bin/python def isLeapYear(a): if (0 == a%4 or 0 == a%400) and 0 != a%100 : return 1 else: return 0 dict = {1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31} a = int(input("Input year:")) b = int(input("Input month:")) c = int(input("Input day:")) m = 0 k = isLeapYear(a) for i in range(1,b): m = m + dict[i] m = m + isLeapYear(a) + c print(m)老虎头
shali1026@163.com
shusihui
511701990@qq.com
参考方法:
#!/usr/bin/env python # -*- coding: utf-8 -*- year = int(raw_input('year:\n')) month = int(raw_input('month:\n')) day = int(raw_input('day:\n')) days = [31,28,31,30,31,30,31,31,30,31,30,31] if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0): days[2] += 1 now = sum(days[0:month-1])+day print 'it is the %dth day.' %nowshusihui
511701990@qq.com
lqy126
412497942@qq.com
参考方案:
#!/usr/bin/env python #coding:utf-8 import time import sys reload(sys) sys.setdefaultencoding('utf-8') # 设置 'utf-8' a = raw_input("输入时间(格式如:2017-04-04):") t = time.strptime(a,"%Y-%m-%d") print time.strftime("今年的第%j天",t).decode('utf-8')lqy126
412497942@qq.com