Python:每日一题 24(答题领鱼币)
小陆每天要写一份工作日报,日报标准是“入职第X天-小陆”,对于“入职第几天”,小陆现在每次需要对上次写的日报标题里的天数+1。请你写一段程序,帮小陆自动完成这件事。
提供写日期当天的年月日,算出已入职的天数(假定小陆的入职时间是 2014年8月18日)。
现在是2017年4月19日,问小陆上班几天了?
要求:不能使用时间,日期相关的库函数。
欢迎小伙伴们,一起答题!
如果你有能力,欢迎加入我们!
已经上车老司机:@冬雪雪冬 @gopythoner @lumber2388779 @ooxx7788
点我上车 占个楼、凑个数、学个习、点个赞、支持下 @冬雪雪冬 @gopythoner @lumber2388779 @ooxx7788
来吧~ 感觉一个好蠢的答案!希望不要见笑,没有考虑双休的情况!看看高手解答!month = {1:31, 2:28, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31}
now_year = int(input('请输入现在何年:'))
now_month = int(input('请输入现在何月:'))
now_day = int(input('请输入现在何日:'))
entry_year = int(input('请输入入职年份:'))
entry_month = int(input('请输入入职月份:'))
entry_day = int(input('请输入入职几号:'))
leap_year = 0
not_leap_year = 0
a = 0
for each_year in range(entry_year + 1, now_year):
if each_year % 400 == 0:
leap_year += 1
else:
not_leap_year += 1
if entry_year % 400 == 0:
month = 29
if entry_month > 1:
for i in range(1, entry_month):
a += month
days_1 = 366 - a -entry_day
a = 0
else:
days_1 = 366 - entry_day
else:
if entry_month > 1:
for i in range(1, entry_month):
a += month
days_1 = 365 - (a + entry_day)
a = 0
else:
days_1 = 365 - entry_day
if now_year % 400 == 0:
month = 29
if now_month > 1:
for i in range(1, now_month):
a += month
days_2 = a + now_day
a = 0
else:
days_2 = now_day
else:
if entry_month > 1:
for i in range(1, now_month):
a += month
days_2 = a + now_day
a = 0
else:
days_2 = now_day
days = days_1 +days_2 + 366 * leap_year + 365 * not_leap_year
print(days) 学习,支持,点赞 先判断闰年,再计算是一年的第几天。
def leapyear(year):
if (year % 4 == 0 and year % 100) or year % 400 == 0:
return True
else:
return False
def days(ymd):
d =(0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30)
day = sum(d[:ymd]) + ymd
if leapyear(ymd) and ymd > 2:
day += 1
return day
start = (2014, 8, 18)
now = (2017, 4, 19)
result = - days(start)
for i in range(start, now):
if leapyear(i):
result += 366
else:
result += 365
result = result + days(now)
print(result) 学习、支持、点赞 这就好比做数学题,明明有三角函数公式可以用,你不准我用,让我去用加减乘除
这题哥哥不考了,交白卷
{:9_227:} 六道土豆 发表于 2017-4-9 21:15
感觉一个好蠢的答案!希望不要见笑,没有考虑双休的情况!看看高手解答!
我没做出来,厉害~ #this file is used to calculate someone attend days
#if someone's entry day is 2014.8.18
#user input random days followed year.month.day format
#the program calculated someone's attend days
print('wonderful enjoy will start soon,please follow below tips accomplish!!')
print('this version start day is 2014.8.18')
print('please input end day')
Y = input('please input years you think:')
M = input('please input months you think:')
D = input('please input days you think:')
GAP_day = 0
temp0_gap_day = 0
temp1_gap_day = 0
temp2_gap_day = 0
leap_year =
nonleap_year =
if int(Y) < 2014 :
print('this is a invalid year number,should greater than 2014,please restart')
elif int(Y) == 2014:
if int(M) == 8:
if int(D) >= 18 :
GAP_day = int(D) - 17
else:
print('warning:day number should greater than 18\nplease input day you think again:')
elif int(M) < 8:
print('warning:month number should greater than 8')
elif int(M) > 8 and int(M) <= 12 :
for i in range(7,int(M)-1):
temp0_gap_day = temp0_gap_day + nonleap_year
GAP_day = temp0_gap_day + int(D) - 17
print('小陆的入职时间是:%d天'%GAP_day)
elif int(Y) > 2014:
for i in range(0,((int(Y)-2014))):
if (2014+i)%4 == 0 and (2014+i)%100 != 0 :
#print('current year:%d'%(2014+i))
#print('this year is a leap year!')
temp1_gap_day += 366
else:
#print('current year:%d'%(2014+i))
#print('this year is a nonleap year!')
temp1_gap_day += 365
#print('first part gap_day is:%d'%temp1_gap_day)
if int(Y)%4 == 0 and int(Y)%100 != 0 :
#print('current year:%d'%int(Y))
#print('this year is a leap year!')
for i in range(0,int(M)-1):
temp2_gap_day = temp2_gap_day + leap_year + int(D)
else:
#print('current year:%d'%int(Y))
#print('this year is a nonleap year!')
for i in range(0,int(M)-1):
temp2_gap_day = temp2_gap_day + nonleap_year + int(D)
#print('second part gap_day is:%d'%temp2_gap_day)
GAP_day = temp1_gap_day + temp2_gap_day - 230
print('小陆的入职时间是:%d天'%GAP_day)
比较繁琐的做法!! rosen 发表于 2017-4-10 21:31
比较繁琐的做法!!
犯了个错误,55句应该为GAP_day = temp1_gap_day + temp2_gap_day - 230 + int(D),
并把47句和53句后面的int(D)删掉。。。。
刚和楼上的对比发现结果不一样,菜鸟多包涵!!! 冬雪雪冬 发表于 2017-4-10 00:00
先判断闰年,再计算是一年的第几天。
你这个有点问题哦{:5_109:},你把当前日期设置和入职日期同样的情况,看看是入职第几天 我是先算年,再是月,最后算日,分别加入入职天数
rn,ry,rr=2014,8,18
dn,dy,dr=2017,4,19
day=0
y=1 if dy-ry>0 else -1
if ry>2:
for i in range(dn-rn):
day+=366 if (i+rn+1)%4==0 and (i+rn+1)%100!=0 else 365
else:
for i in range(dn-rn):
day+= 366 if (i+rn)%4==0 and (i+rn)%100!=0 else 365
for i in range(dy-ry if y>0 else ry-dy):
if (ry+i)%12 in :
day+=31*y
elif (ry+i)%12 ==2:
day+=(29 if dn%4==0 and dn%100!=0 else 28)*y
else:day+=30*y
day+=(dr-rr+1)
print(day) 余欲渔 发表于 2017-4-11 11:28
你这个有点问题哦,你把当前日期设置和入职日期同样的情况,看看是入职第几天
说的有道理,我是计算的是两个日期的差值,按照题意应该再加一。 冬雪雪冬 发表于 2017-4-11 11:32
说的有道理,我是计算的是两个日期的差值,按照题意应该再加一。
{:10_256:}你竟然出错了 新手·ing 发表于 2017-4-11 15:49
你竟然出错了
{:10_269:} 朝闻夕死 发表于 2017-4-9 15:48
占个楼、凑个数、学个习、点个赞、支持下
这水有水平 我服你
为什么大家都不注释,看起来有些蛋疼···{:10_250:}
months =
start =
now =
# 判断两个日期是该年第几天,忽略闰年
s_day = sum(months[:start-1]) + start
n_day = sum(months[:now-1]) + now
# 判断是否闰年的函数
judge = lambda y: 1 if ((y%100 !=0 and y%4==0) or (y%400==0)) else 0
# 第二年到去年之间的闰年有几个(如果有)
leap_days = list(map(judge, range(start+1,now)))
# 判断第一年和今年,若是闰年根据日期决定如何处理2月29日,要避免在同一年重复计算
s_day -= judge(start) if s_day<=59 else 0
n_day += judge(now) if n_day>59 and now!=start else 0
# 入职天数全部加起来(没有第0天,要+1)
day_num = n_day - s_day+1 + (now-start)*365 + sum(leap_days)
print("入职第 %d 天,blah blah blah..." % day_num) def leap(year):
if (year%4==0 and year%100==0) or year%400==0:
return True
else:
return False
def days(day,now):
d =
years = now-day
ndays = sum(d])+now
ddays = sum(d])+day
if leap(now) and now>2:
ydays = years*365+1
else:
ydays = years*365
return ydays+ndays-ddays+1
if __name__ == '__main__':
now1 = (2017,4,19)
day1 = (2014,8,18)
print('小陆入职第%d天了'%days(day1,now1))
Month =
# If leap year, then return True; Or return False
def IsLeapYear(year):
if ((year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)):
return True
else:
return False
def DateToNumber(date):
year = date
month = date
day = date
months = sum(i * Month for i in range(1, month))
# Get the days of Month
if (True == IsLeapYear(year) and month > 1):
months =months + 1
# Get the total days of the date
days = year * 365 + months + day
return days
CurrentDate =
EntryDate =
print DateToNumber(CurrentDate) - DateToNumber(EntryDate)