鱼C论坛

 找回密码
 立即注册
楼主: 新手·ing

[技术交流] Python:每日一题 4

[复制链接]
发表于 2017-8-12 17:31:26 | 显示全部楼层
  1. str1 = input('请输入某年某月某日(格式为YYYY-MM-DD):')
  2. YY = str1[0:4]
  3. MM = str1[5:7]
  4. DD = str1[8:10]
  5. day_list = [31,28,31,30,31,30,31,31,30,31,30,31] # 每个月的天数,从1月份开始

  6. day_list = day_list[:int(MM)-1]
  7.          
  8. days = sum(day_list) + int(DD)

  9. if (int(YY)% 4 == 0 and int(YY) % 100 != 0)  or (int(YY) % 400 == 0): # 判断闰年
  10.     if int(MM) > 2 :
  11.         days +=1
  12.    
  13. print('这一天是这一年的第%d天' % days)
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-18 09:58:56 | 显示全部楼层
  1. import time
  2. print("-----------输入年月日,判断这天是这一年的第几天------------")


  3. date = input("请输入日期,格式为YYYY-MM-DD:")
  4. temp = time.strptime(date,"%Y-%m-%d")
  5. year,month,day = temp[0:3]

  6. DaysPerMonth = [31,28,31,30,31,30,31,31,30,31,30,31]
  7. daysnumber = 0

  8. for i in range(0,month):
  9.     daysnumber += DaysPerMonth[i]

  10. if year%400 ==0:
  11.     daysnumber = daysnumber + 1
  12. else:
  13.     if year%100 != 0 and year%4 == 0:
  14.         daysnumber = daysnumber + 1


  15. print("这是 %d 年的第 %d 天" %(year,daysnumber))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-20 17:23:50 | 显示全部楼层
  1. year = int(input('请输入年份:'))
  2. month = int(input('请输入月份:'))
  3. day = int(input('请输入日期:'))
  4. days = [31,28,31,30,31,30,31,31,30,31,30,31]
  5. n = 0
  6. for i in range(month - 1):
  7.     n += days[i]
  8. if ((year % 4 == 0 and year % 100 != 0) or year % 400 == 0) and month > 2:
  9.     n = n + day + 1
  10. else:
  11.     n += day
  12. print('这一天是%s年的第%s天。' % (year,n))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-22 23:50:02 | 显示全部楼层
print ('_____判断一年的第几天______')
year=int(input('请输入年份'))
month=int(input('请输入月份'))
day=int(input('请输入日'))
months1=[0,31,59,90,120,151,181,212,243,273,304,334]
months2=[0,31,60,91,121,152,182,213,244,274,305,335]
result =0
if year%4==0:
    temp = month-1
    result=months2[temp]+day
    print('这是这个年份的第',result,'天')
else:
    temp = month-1
    result=months1[temp]+day
    print('这是这个年份的第',result,'天')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-22 23:50:48 | 显示全部楼层
  1. print ('_____判断一年的第几天______')
  2. year=int(input('请输入年份'))
  3. month=int(input('请输入月份'))
  4. day=int(input('请输入日'))
  5. months1=[0,31,59,90,120,151,181,212,243,273,304,334]
  6. months2=[0,31,60,91,121,152,182,213,244,274,305,335]
  7. result =0
  8. if year%4==0:
  9.     temp = month-1
  10.     result=months2[temp]+day
  11.     print('这是这个年份的第',result,'天')
  12. else:
  13.     temp = month-1
  14.     result=months1[temp]+day
  15.     print('这是这个年份的第',result,'天')
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-4 23:58:08 | 显示全部楼层
  1. month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

  2. def DetermineDateOfYear(date):
  3.     # To Determine the days of month
  4.     IntOfMonth = int(date[4:6])
  5.     for i in range(IntOfMonth - 1):
  6.         DaysOfMonth = (i + 1) * month[i]
  7.     # To Determine Leap year
  8.     IntOfYear = int(date[0:4])
  9.     if ((IntOfYear % 4 == 0) and (IntOfYear % 100 != 0)) or (IntOfYear % 400 == 0):
  10.         TotalDays = DaysOfMonth + 1 + int(date[6:8])
  11.     else: # Is not leap year
  12.         TotalDays = DaysOfMonth + int(date[6:8])
  13.     return TotalDays

  14. DATE = raw_input("Enter the date(19700701):")
  15. TotalDATE = DetermineDateOfYear(DATE)
  16. print "The days of DATE is %d" % TotalDATE
复制代码


太复杂了,之前学的C。。。。明天用python重新写一个。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-11 11:03:47 | 显示全部楼层
  1. def calday(month, day):
  2.     listm = [31,28,31,30,31,30,31,31,30,31,30]
  3.     result = 0

  4.     for each in listm[0:month-1]:
  5.         result += each

  6.     result += day
  7.     return result

  8. flage = True
  9. while flage:
  10.    
  11.     year = int(input('Please enter the year:'))
  12.     month = int(input('Please enter the month:'))
  13.     day = int(input('Please enter the month:'))

  14.     days = calday(month,day)
  15.     if (year % 400 == 0) or (year % 100 and year%4 == 0):
  16.         days += 1

  17.     print('In ' + str(year) + ', ' + str(month) + '-' + str(day) + ' is the ' + str(days) + 'th day')

  18.     flage = str(input('Try again?(q to quit):'))
  19.     if flage in ['q','Q']:
  20.         print('ByeBye')
  21.         flage = False
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-17 22:55:04 | 显示全部楼层
用datetime模块实现。有模块用模块!
  1. # -*-coding:gbk-*-
  2. __author__ = 'chennan'
  3. import datetime
  4. x=int(input("请输入年份xxxx:"))
  5. y=int(input("请输入月份xx:"))
  6. z=int(input("请输入日xx:"))
  7. n1=datetime.date(x,y,z)
  8. n2=datetime.date(x,1,1)
  9. n3=n1-n2
  10. n3=int(n3.days)+1
  11. print("这是这一年的第%s天"%n3)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-23 21:34:59 | 显示全部楼层
n=int(input('year:'))
y=int(input('month:'))
r=int(input('day:'))
x=0
if n%4==0 and n%100!=0:
    two=29
else:
    two=28
for i in range(1,y):
    if i in[1,3,5,7,8,10,12]:
        x+=31
    elif i==2:
        x+=two
    else:
        x+=30
print('%d月%d号是%d年的第%d天'%(y,r,n,(x+r)))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-1 16:35:59 | 显示全部楼层
  1. def isRunnian(year):
  2.     return (year%4==0 and year%100!=0) or year%400 == 0

  3. def theDay(year, month, date):
  4.     if isRunnian(year):
  5.         daysOfMonth = [31,29,31,30,31,30,31,31,30,31,30,31]
  6.     else:
  7.         daysOfMonth = [31,28,31,30,31,30,31,31,30,31,30,31]
  8.     days = sum(daysOfMonth[:month-1]) + date
  9.     print('%s年%s月%s日是这一年的第%s天' % (year, month, date, days))

  10. theDay(2017,12,1)

  11. #  2017年12月1日是这一年的第335天
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-19 21:37:47 | 显示全部楼层

怎么我把这个程序输进去,没有结果出来啊。好奇怪的。第6行那里不太理解,麻烦楼主给讲解一下吧。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-19 21:52:53 | 显示全部楼层
Wofficre 发表于 2017-5-26 12:48
print("请输入某年某月某日")
a=int(input())
b=int(input())

怎么我输进去这个程序,输入的是2018年1月和2月的还正确,3月之后就计算不正确了。2018年3月2日输出的是92天。2018年4月2日就输出是182天了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-12 13:36:26 | 显示全部楼层
y=int(input('input year:'))
m=int(input('input mounth:'))
d=int(input('input day:'))

list1=[31,28,31,30,31,30,31,31,30,31,30,31]
for i in range(0,1000):
        if 2012 + (4 * i) == 2012 or 2012 - (4 * i) == 2012:
                list1[1]=29
               
days=0
for j in range(0 , m+1):
        days+=list1[j]

days=days-31+d
print(days)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-16 20:18:29 | 显示全部楼层
源稚空 发表于 2017-4-17 21:20
year = int(input('year = '))
month = int(input('month = '))
day  = int(input('day = '))

方法很好,只是有点小瑕疵,应将today改为:today = [0,31,28,31,30,31,30,31,31,30,31,30,31],today[0] = 0
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-16 22:32:57 | 显示全部楼层
  1. def isT(year):
  2.         if((year % 100 != 0 and year % 4 == 0) or year % 400 == 0):
  3.                 return True
  4.         else:
  5.                 return False

  6. mouths   = [12,11,10,9,8,7,6,5,4,3,2,1]
  7. days     = [31,30,31,30,31,31,30,31,30,30,28,31]
  8. year     = int(input("please enter year:"))
  9. mouth    = int(input("please enter mouth"))
  10. day      = int(input("please enter day"))
  11. flag     = False
  12. sum_days = 0

  13. for i in range(12):
  14.         if (mouth > mouths[i]):
  15.                 sum_days += days[i]


  16. if(isT(year)):
  17.         print(sum_days + day + 1)
  18. else:
  19.         print(sum_days + day)


复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-17 15:43:34 | 显示全部楼层

import datetime

date=input('年月日(比如:20130101):')

sli=date[:4]+'0101'         
stop=datetime.datetime.strptime(date,'%Y%m%d')
start=datetime.datetime.strptime(sli,'%Y%m%d')

x=(stop-start).days

print('这是此年的第{}天'.format(x+1))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-25 18:11:10 | 显示全部楼层
'''题目:输入某年某月某日,判断这一天是这一年的第几天?
程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于2时需考虑多加一天'''
# 一年有12个月,其中1月、3月、5月、7月、8月、10月、12月为31天;4月、6月、9月、11月为30天;2月为28天(闰年为29天)
# 闰年是公历中的名词,能被4整除但不能被100整除,或能被400整除的年份即为闰年。闰年366天
str1 = input('please input a certain day of the year.eg:xxxx年xx月xx日')
month = int(str1[5] + str1[6])
Today = int(str1[8] + str1[9])
year = int(str1[0] + str1[1] + str1[2] + str1[3])
# list_month=[1,2,3,4,5,6,7,8,9,10,11,12]
day = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30]
r = 0
for i in range(0, 13):
    if (month >= i+1):
        r += day[i]
r += Today
if (year % 400 == 0):
    print(str1, '是今年的第', r + 1, '天')
else:
    print(str1, '是今年的第', r, '天')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-25 18:11:54 | 显示全部楼层
  1. '''题目:输入某年某月某日,判断这一天是这一年的第几天?
  2. 程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于2时需考虑多加一天'''
  3. # 一年有12个月,其中1月、3月、5月、7月、8月、10月、12月为31天;4月、6月、9月、11月为30天;2月为28天(闰年为29天)
  4. # 闰年是公历中的名词,能被4整除但不能被100整除,或能被400整除的年份即为闰年。闰年366天
  5. str1 = input('please input a certain day of the year.eg:xxxx年xx月xx日')
  6. month = int(str1[5] + str1[6])
  7. Today = int(str1[8] + str1[9])
  8. year = int(str1[0] + str1[1] + str1[2] + str1[3])
  9. # list_month=[1,2,3,4,5,6,7,8,9,10,11,12]
  10. day = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30]
  11. r = 0
  12. for i in range(0, 13):
  13.     if (month >= i+1):
  14.         r += day[i]
  15. r += Today
  16. if (year % 400 == 0):
  17.     print(str1, '是今年的第', r + 1, '天')
  18. else:
  19.     print(str1, '是今年的第', r, '天')
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-25 18:23:11 | 显示全部楼层

day2 = sum(days[:month - 1]) + day
这一行什么意思?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-25 18:32:26 | 显示全部楼层

print('这是 %s 年的第 %s 天!' % (year,sum))
%d是什么来着?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 16:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表