首先,下面的题目来自个人大学同窗的分享,他用数学证实,我用编程计算机发现了答案。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