首先,下面的題目來自個人大學同窗的分享,他用數學證實,我用編程計算機發現了答案。git
他的數學推理:編程
而後下面是個人Python代碼:spa
#coding=utf-8 # 井的高度 well_hegith = 12 print ('井的高度是%ld'%(well_hegith)) # 白天 def daytime(allHeight): allHeight = allHeight + 5 if allHeight>well_hegith: allHeight = -1 else: allHeight = allHeight * 0.8 return allHeight # 黑夜 def night(allHeight): allHeight = allHeight + 3 if allHeight>well_hegith: allHeight = -1 else: allHeight = allHeight*2.0/3.0 return allHeight # 白天開始算,是否是白天,默認開始是白天 isDayTime = True # 總的高度,默認開始是0 allHeight = 0 # 天數 days = 0.0 while allHeight >= 0: print days days = days + 0.5 if isDayTime: allHeight = daytime(allHeight) isDayTime = False else: allHeight = night(allHeight) isDayTime = True print ('高度:=======%lf'%(allHeight))
個人程序最後跑出來進入了無限循環中。。。3d