菜鸟教程 -- 学的不仅是技术,更是梦想!

Python 基础教程
(追記) (追記ここまで)

Python 练习实例20

Python 100例 Python 100例

题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?

程序分析:

程序源代码:

Python 实例 1

#!/usr/bin/python# -*- coding: UTF-8 -*-tour = []height = []initial_height = 100.0# 初始高度num_bounces = 10# 弹跳次数foriinrange(1, num_bounces + 1): # 从第二次开始,落地时的距离应该是反弹高度乘以2(弹到最高点再落下)ifi == 1: tour.append(initial_height)else: tour.append(2 * initial_height)initial_height /= 2height.append(initial_height)total_distance = sum(tour)final_bounce_height = height[-1]print(f"总高度:tour = {total_distance}")print(f"第{num_bounces}次反弹高度:height = {final_bounce_height}")

以上实例输出结果为:

总高度:tour = 299.609375
第10次反弹高度:height = 0.09765625

实例 2

def calculate_distance_and_height(height, times):
total_distance = 0
current_height = height

for i in range(1, times + 1):
# 从第二次开始,落地时的距离应该是反弹高度乘以2(弹到最高点再落下)
if i == 1:
total_distance += current_height
else:
total_distance += current_height * 2 # 每次下落和反弹的距离
current_height /= 2 # 反弹后的高度

return total_distance, current_height

# 初始高度为100米,计算第10次落地时的总距离和反弹高度
height = 100
bounce_times = 10
total_distance, final_height = calculate_distance_and_height(height, bounce_times)

print(f"第{bounce_times}次落地时,共经过 {total_distance} 米。")
print(f"第{bounce_times}次反弹的高度为 {final_height} 米。")

以上实例输出结果为:

第10次落地时,共经过 299.609375 米。
第10次反弹的高度为 0.09765625 米。

Python 100例 Python 100例

AI 思考中...

11 篇笔记 写笔记

  1. #0

    健健

    459***[email protected]

    32

    Python3 参考方案:

    #!/usr/bin/python3
    hei = 100 # 总高度
    tim = 10 # 次数
    height = [] # 每次反弹高度
    for i in range(2,tim+1): # 计算第二次落地到第十次落地
     hei /= 2
     height.append(hei)
    print('第10次落地时,反弹%s高'%(min(height)/2)) # 第十次反弹为第十次落地距离的一半
    print('第10次落地时,经过%s米'% (sum(height)*2+100)) # 总和加上第一次的 100
    

    输出结果:

    第10次落地时,反弹0.09765625高
    第10次落地时,经过299.609375米
    

    健健

    459***[email protected]

    9年前 (2017年04月22日)
  2. #0

    james

    214***[email protected]

    3

    Python3 参考方案:

    #!/usr/bin/python3
    l=[]
    r=10
    t=100 # 第一次落地经过距离
    sum=0
    while r>1: # 计算第二次落地到第十次落地每次的高度
     t=t/2
     r=r-1
     l.append(t)
    for k in range(0,9): # 列表只有 9 条数据
     if k==8:
     print(l[k]/2) # 第10次反弹高度
     sum+=l[k]
    sum=sum*2
    sum=sum+100
    print(sum)

    james

    214***[email protected]

    9年前 (2017年04月26日)
  3. #0

    等一个人

    252***[email protected]

    3

    Python3 测试实例:

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    sum = 0
    total = 0
    for i in range(1, 10):
     sum = (100 * 2) / (2 ** i)
     total += sum
    result = 100 + total
    tenth = 100 / (2 ** 10)
    print('第10次反弹高度: {}'.format(tenth))
    print('第10次反弹后,一共经历的距离: {}'.format(result))
    

    等一个人

    252***[email protected]

    9年前 (2017年04月28日)
  4. #0

    fancy

    249***[email protected]

    0

    参考方法:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    m=100
    n=input("请输入反弹次数:")
    total=[]
    l=[]
    for i in range(1,n+1):
     if i==1:
     total.append(m)
     else:
     total.append(2*m)
     m=0.5*m
     l.append(m)
    print l
    print total
    print "第%d次反弹的高度是:%f"%(n,l[n-1])
    print "第%d次落地共经过%f米"%(n,sum(total))
    

    fancy

    249***[email protected]

    9年前 (2017年05月10日)
  5. #0
    6

    Python3 参考方法:

    #!/usr/bin/env python3
    long = 100
    sum = 100
    for i in range(2, 11):
     sum = sum + long
     long = long / 2
     print("第%d次,经过%f米,反弹高度为%f" % (i, sum, long/2))
    
    9年前 (2017年05月23日)
  6. #0

    轮子着火了

    yun***[email protected]

    4

    参考手册:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    # h 为初始高度,k 为每次弹起的高度比例,如本题弹起一半即为 0.5,n 为反弹次数
    def Sumh(h,k,n):
     L = []
     for i in range(1,n+1):
     h *= k
     totalh = h * 3
     L.append(totalh)
     print h
     print sum(L) - h # 第 10 次落地高度,要去除最后一次反弹
    Sumh(100,0.5,10)
    

    轮子着火了

    yun***[email protected]

    9年前 (2017年05月23日)
  7. #0

    Think-dfrent

    iwa***[email protected]

    0

    Python3 参考方法:使用递归生成每一次反弹的高度,其中参数为第几次反弹,如n=1时,第一次反弹高度为height(1)=50

    #!/usr/bin/python3
    def height(n):
     if n==0 :
     return 100
     else:
     return height(n-1)/2
    sum=0
    count=10
    for i in range(0,count):
     if i==0:
     sum=sum+height(i)
     else:
     sum=sum+2*height(i)
     #print(height(i))
    print(sum)
    print(height(10))

    Think-dfrent

    iwa***[email protected]

    9年前 (2017年06月16日)
  8. #0

    Mervyn

    110***[email protected]

    0

    参考方法:

    a = 100.00
    b=0.0
    print a/(2**10)
    for i in range(0,10):
     b,a = b+2*a, a/2
    print b -100
    

    Mervyn

    110***[email protected]

    9年前 (2017年08月16日)
  9. #0

    小差罗

    675***[email protected]

    0

    Python3 测试实例:

    h=100
    t=10
    height=[100]
    for i in range(t):
     height.append(h)
     h=h/2
    print(height)
    print('总高度:',sum(height[:10]),'第10次反弹高度 height[10]:',height[10]/2)

    小差罗

    675***[email protected]

    9年前 (2017年09月12日)
  10. #0

    如花

    yiy***[email protected]

    1

    参考方法:

    from __future__ import division
    height = 100
    n = 10
    tour = 0
    psum = pow(2,10)
    bnce10 = 100/psum
    print bnce10
    for i in range(1,10):
     #print tour
     tour += 2*(100/pow(2,i))
    tour = height + tour
    print "tour=%f"%tour

    如花

    yiy***[email protected]

    9年前 (2017年09月13日)
  11. #0

    忧伤的中指

    fin***[email protected]

    0

    兼容 Python3.x 与 Python2.x:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    import math
    # 设落地n次
    n = int(input("请输入反弹次数:"))
    height = 100
    print("第 {} 次落地共: {:<8}米".format(n,height*(3-math.pow(2,-(n-2)))))
    print("第 {} 次 反 弹: {:<8}米".format(n,height*math.pow(2, -(n))))

    忧伤的中指

    fin***[email protected]

    9年前 (2017年10月20日)

点我分享笔记

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址

AltStyle によって変換されたページ (->オリジナル) /