0
ans = num_rushes(100, 15, 7)
print(ans) 

应该返回 19 但我的返回 12?

这是我所拥有的:

def num_rushes(slope_height, rush_height_gain, back_sliding):
    rushes = 0
    current_height = 0
    rush_height_counter = 0
    back_sliding_counter = 0
    rush_height_gain = 0.95 ** rush_height_counter * rush_height_gain
    back_sliding = 0.95 ** back_sliding_counter * back_sliding

    while current_height < slope_height:
        current_height = current_height + rush_height_gain
        rushes += 1
        rush_height_counter += 1
        back_sliding_counter += 1

        if current_height < slope_height:
            current_height = current_height - back_sliding

        else:
            return rushes
4

1 回答 1

2

这段代码0.95 ** rush_height_counter * rush_height_gain你的意思是

0.95 ** (rush_height_counter * rush_height_gain)

或者

(0.95 ** rush_height_counter) * rush_height_gain

我想你想要第一个,但 python(据我所知)做第二个!

于 2013-08-20T11:55:03.263 回答