You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何基于Coefficient of Restitution计算总行程的代码结果错误?

问题原因分析:漏掉了弹跳的下落阶段

Ah, I spot the mistake right away! Your code only accounts for the upward bounce height after the initial drop, but it's missing the downward fall that happens after every upward bounce (except possibly the very last one if it's too low to count).

Let's break this down with your test case (cor=0.7, h=8):

  1. The initial drop is 8 meters — you got this right by setting th = h.
  2. First bounce: the object goes up 5.6 meters, then falls back down 5.6 meters — your code only adds the 5.6m upward part, not the downward part.
  3. This repeats for every bounce until the upward height drops below 0.1m. For bounces where the upward height is still ≥0.1m, you need to add twice the bounce height (up + down). Only the final bounce (which goes below 0.1m) should just add the upward height (since it doesn't fall back down).

Looking at your original code:

cor = float(input("Enter coefficient of restitution : "))
h = float(input("Enter initial height in meters : "))
nob = 0
th = h  # Correct: initial drop
while (h >= 0.10):
    nob += 1
    h *= cor  # Calculate upward bounce height
    th += h  # Only adds upward height, missing downward fall!

Fixing the Code

We need to adjust how we add to the total distance: for each bounce where the resulting height is still ≥0.1m, add twice the bounce height. For the final bounce (where height <0.1m), just add the upward height. Here's the corrected version:

cor = float(input("Enter coefficient of restitution : "))
initial_h = float(input("Enter initial height in meters : "))
nob = 0
total_distance = initial_h  # Start with the initial drop
current_h = initial_h

while True:
    bounce_h = current_h * cor
    nob += 1
    if bounce_h < 0.1:
        total_distance += bounce_h  # Only add upward, no fall back
        break
    total_distance += 2 * bounce_h  # Add up + down
    current_h = bounce_h

print("Meters traveled : {0:.2f}".format(total_distance))
print("Number of bounces : ", nob)

Testing this with cor=0.7 and initial_h=8 gives 44.82 meters (matching your textbook) and 13 bounces (which your code already got right).

Why Your Bounce Count Was Correct

Your loop counts every time you calculate a bounce (even the final one that's below 0.1m), which aligns with how the textbook counts bounces — so that part was never broken! The only issue was missing those downward legs of the bounces.

内容的提问来源于stack exchange,提问作者Plouton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:02:54