Python exercise 015: a free landing ball, python015
[Python exercise 015]A ball falls freely from the height of 100 meters. After each landing, the ball jumps back to half of the original height and then falls. How many meters did it pass when it landed for 10th times? How high is the rebound of 10th times?
----------------------------------------------------------
In fact, this question can be written, because it is implemented 10 times. The results provided on the Internet are also varied, and you do not know whether your calculations are correct. Please enlighten us.
My idea is: Except for 1st drops from the height of 100, the other 9 times have gone through the "rebound-landing" process, the length of each process is equal to the height of the previous process. Therefore, initialize and assign values to the 1st processes. Starting from the 2nd processes, calculate the height of the previous process first, then halved the height, and pass it to the next process for calculation. The Code is as follows:
A = [100] # the course of each 'bounce landing 'process, only landing for 1st times (100 meters) h = 100 # Each 'bounce landing' process, the height of the bounce, 1st times for 100 m print ('100 times from % s high landing, walked through % s meter, then rebounded to % s meter. '% (H, a [0], h/2) for I in range (1st): # Times initialized, 9 times recycled. append (h) # calculate the distance first, and then halved the height, because a 'bounce landing 'is two heights h = h/2 print (' % s landing from % s high, Total % s meters, then it rebounded to % s meters. '% (I, h, sum (a), h/2 ))
The output result is as follows:
1st times from 100 high landing, walked over 100 meters, and then rebounded to 50.0 meters.
2nd times from 50.0 high landing, a total of 200 meters, and then rebounded to 25.0.
3rd Times from 25.0 high landing, a total of 250.0 meters, and then rebounded to 12.5.
4th times from 12.5 high landing, a total of 275.0 meters, and then rebounded to 6.25.
5th times from 6.25 high landing, a total of 287.5 meters, and then rebounded to 3.125.
6th times from 3.125 high landing, a total of 293.75 meters, and then rebounded to 1.5625.
7th times from 1.5625 high landing, a total of 296.875 meters, and then rebounded to 0.78125.
8th times from 0.78125 high landing, a total of 298.4375 meters, and then rebounded to 0.390625.
9th times from 0.390625 high landing, a total of 299.21875 meters, and then rebounded to 0.1953125.
10th times from 0.1953125 high landing, a total of 299.609375 meters, and then rebounded to 0.09765625.
That is to say: when the first landing, a total of 10th meters, 299.609375 times rebounded to the height of 11th.
++
Source: getting started with programming languages: 100 typical examples [Python]