Crossin的编程教室
标题: 题目:输入某年某月某日,判断这一天是这一年的第几天? [打印本页]
作者: fangweiren 时间: 2016-2-6 14:51
标题: 题目:输入某年某月某日,判断这一天是这一年的第几天?
@cross先生
下面程序中“if 0 <= month <= 12:”应该不对吧,0应该不包含,对吧
程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊 情况,闰年且输入月份大于3时需考虑多加一天:
程序源代码:
- #!/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
复制代码 这里的python100题挺不错的
http://www.2cto.com/kf/web/python/sl/
作者: crossin先生 时间: 2016-2-7 15:45
是的
作者: ly_911004 时间: 2016-3-14 16:01
- import time,datetime
- # 2016-03-14
- months = (1,3,5,7,8,10,12)
- date = raw_input('请输入要查询的日期("最好以'+'"-"'+'分割"):')
- stime = time.time()
- datetime1 = datetime.datetime.strptime(date,'%Y-%m-%d')
- try:
- year = datetime1.year
- month = datetime1.month
- day = datetime1.day
- if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
- print 'the day is %d 天'%(months.index(month) * 31 + (month > 2 and 29 + (month - months.index(month) - 2) * 30 or 0) + day)
- etime = time.time()
- print '代码处理用时:%f'%(etime-stime)
- else:
- print 'the day is %d 天'%(months.index(month) * 31 + (month > 2 and 28 + (month - months.index(month) - 2) * 30 or 0) + day)
- etime = time.time()
- print '代码处理用时:%f' %(etime-stime)
- except:
- print '请按照正确的时间格式输入'
复制代码
作者: ly_911004 时间: 2016-3-14 16:03
总感觉我的哪儿不对
作者: crossin先生 时间: 2016-3-14 21:41
ly_911004 发表于 2016-3-14 16:03
总感觉我的哪儿不对
测试跑一下就知道对不对了
欢迎光临 Crossin的编程教室 (https://bbs.crossincode.com/) |
Powered by Discuz! X2.5 |