我有一个while循环,该循环返回爬山所需的“奔波”次数。小山的大小是“斜坡高度”,爬升的高度是“ rush_height_gain”减去“ back_sliding”。
以下代码适用于:
ans = num_rushes(15, 10, 5)
print(ans)
打印1
和
ans = num_rushes(100, 15,7)
print(ans)
打印2
和
ans = num_rushes(10, 10, 9)
print(ans)
打印12
但是返回错误的答案
ans = num_rushes(100, 10, 0)
print(ans)
应该打印10,但是打印9
我不确定为什么会这样,任何帮助将不胜感激
def num_rushes(slope_height, rush_height_gain, back_sliding):
current_height = 0
rushes = 0
while current_height < slope_height:
if rush_height_gain == slope_height:
rushes+=1
return rushes
elif current_height < slope_height:
if current_height == slope_height:
return rushes
else:
a = rush_height_gain - back_sliding
current_height += a
if current_height == slope_height:
return rushes
elif current_height > slope_height:
return rushes
else:
rushes+=1
return (rushes)
慕斯709654
犯罪嫌疑人X
呼如林
相关分类