4 回答

TA贡献1840条经验 获得超5个赞
如果我正确地理解了这个问题,那么我认为您正在寻找的是:
def num_rushes(slope_height, rush_height_gain, back_sliding):
if rush_height_gain < slope_height and rush_height_gain - back_sliding < 1:
raise Exception("this is not going to work very well")
current_height = rushes = 0
while current_height < slope_height:
rushes += 1
current_height += rush_height_gain
if current_height >= slope_height:
break
current_height -= back_sliding
return rushes
每次上坡“奔波”后,您都要检查是否已经到达山顶。如果是这样,那么您就完成了;如果没有,那么请向下滑动一下再走一次!正如@perreal在他对原始帖子的评论中的链接中所指出的那样,如果您滑倒的次数多于滑倒的次数,并且第一次没有完全站起来,那么您将会遇到问题。在这些情况下,您可能想抛出一个异常。

TA贡献2080条经验 获得超4个赞
我相信问题是这样的:
if current_height == slope_height:
return rushes
如果back_sliding是0,那么在第十次迭代,current_height从去90到100。然后,支票返回true,并9在递增之前返回。

TA贡献1798条经验 获得超3个赞
def num_rushes(slope_height, rush_height_gain, back_sliding):
current_height = 0
rushes = 0
while current_height < slope_height:
current_height += rush_height_gain
rushes += 1
if current_height < slope_height:
current_height -= back_sliding
return rushes
添加回答
举报