4 回答

TA貢獻(xiàn)1840條經(jīng)驗(yàn) 獲得超5個(gè)贊
如果我正確地理解了這個(gè)問題,那么我認(rèn)為您正在尋找的是:
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
每次上坡“奔波”后,您都要檢查是否已經(jīng)到達(dá)山頂。如果是這樣,那么您就完成了;如果沒有,那么請向下滑動一下再走一次!正如@perreal在他對原始帖子的評論中的鏈接中所指出的那樣,如果您滑倒的次數(shù)多于滑倒的次數(shù),并且第一次沒有完全站起來,那么您將會遇到問題。在這些情況下,您可能想拋出一個(gè)異常。

TA貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個(gè)贊
我相信問題是這樣的:
if current_height == slope_height:
return rushes
如果back_sliding是0,那么在第十次迭代,current_height從去90到100。然后,支票返回true,并9在遞增之前返回。

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超3個(gè)贊
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
添加回答
舉報(bào)