python2.7 exercises (XIX)

19): Title: a ball falls freely from a height of 100 meters, and jumps back to half of its original height after landing each time. Then, how many meters does it pass in the 10th landing? How high is the 10th bounce?

#!/usr/bin/python
# -*- coding: UTF-8 -*-

tour = []
height = []
 
hei = 100.0 ා starting height
 tim = 10 times
 
for i in range(1, tim + 1):
    #From the second time, the landing distance should be the rebound height multiplied by 2 (bounce to the highest point and then fall)
    if i == 1:
        tour.append(hei)
    else:
        tour.append(2*hei) 
    hei /= 2
    height.append(hei)
 
print('total height: tour = {0}'.format(sum(tour)))
print('10th bounce height: height = {0}'.format(height[-1]))

The output result of the above example is:

Total height: tour = 299.609375
 The 10th rebound height: height = 0.09765625

Python 3 reference:

#!/usr/bin/python3

hei = 100         # Total height
tim = 10          # frequency
height = []       # Height per bounce
for i in range(2,tim+1):  # Calculate the second landing to the tenth landing
    hei /= 2
    height.append(hei)
print('Rebound on landing for the 10th time%s high'%(min(height)/2))        # The 10th rebound is half of the 10th landing distance
print('The 10th landing, passing%s rice'% (sum(height)*2+100))   # Sum plus the first 100

The output result is:

When landing for the 10th time, rebound 0.09765625 high
 When landing for the 10th time, after 299.609375m

Python 3 reference:

#!/usr/bin/python3

l=[]
r=10
t=100             # First landing distance
sum=0
while r>1:        # Calculate the height from the second landing to the tenth landing
    t=t/2
    r=r-1
    l.append(t)
for k in range(0,9):       # There are only 9 data in the list
    if k==8:
        print(l[k]/2)        # 10th bounce height
    sum+=l[k]
sum=sum*2
sum=sum+100
print(sum)

Python 3 test example:

#!/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('10th bounce height: {}'.format(tenth))
print('The total distance after the 10th rebound: {}'.format(result))
    
#!/usr/bin/python
# -*- coding: UTF-8 -*-

m=100
n=input("Please enter the number of bounces:")
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 "The first%d The height of the secondary bounce is:%f"%(n,l[n-1])
print "The first%d Total landing passes%f rice"%(n,sum(total))

Python 3 reference method:

#!/usr/bin/env python3

long = 100
sum = 100
for i in range(2, 11):
    sum = sum + long
    long = long / 2
    print("The first%d Second pass%f M, rebound height is%f" % (i, sum, long/2))
    
#!/usr/bin/python
# -*- coding: UTF-8 -*-

# h is the initial height, k is the height ratio of each bounce. If half of the bounce is 0.5, n is the number of bounces
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  # The 10th landing height, to remove the last rebound
Sumh(100,0.5,10)

Python 3 reference method: use recursion to generate the height of each bounce, where the parameter is the number of bounces, for example, when n=1, the height of the first bounce is 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))
    
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

Python 3 test example:

h=100
t=10
height=[100]
for i in range(t):
    height.append(h)
    h=h/2
print(height)
print('Total height:',sum(height[:10]),'10th bounce height height[10]:',height[10]/2)
    
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

Compatible with Python 3. X and python 2. X:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import math

# Set landing n times

n = int(input("Please enter the number of bounces:"))
height = 100
print("The first  {} Secondary landing: {:<8}rice".format(n,height*(3-math.pow(2,-(n-2)))))
print("The first  {} Secondary rebound: {:<8}rice".format(n,height*math.pow(2, -(n))))
This physics is not good. It's going to take a lot of brains. If you feel good, please support me...

Keywords: Python

Added by tsapat on Sun, 05 Apr 2020 09:50:42 +0300