Python exercises 015: A free-standing ball

Source: Internet
Author: User

"python exercise 015" a ball from the height of 100 meters free fall, each landing after jumping back to the original height of half, and then fall. How many meters did it pass when it landed on the 10th time? How high is the 10th time rebound?

----------------------------------------------------------

This problem can actually be written calculation out, because it is 10 times the ground. Look at the results on the internet is also a variety of, do not know whether they are correct, please the great god enlighten.

My idea is: In addition to the 1th time from the height of 100 meters, the remaining 9 times have experienced a "rebound-landing" process, the length of each process is equal to the height of the previous process. Therefore, the 1th process is initialized and assigned, starting from the 2nd process, the height of the previous process is calculated, then the height is halved and passed to the next process for calculation. The code is as follows:

A = [100]  #每个 ' bounce-to-ground ' process passes, the 1th time only to land on the ground (100 meters) H = the  "rebound" process, the height of the rebound, the 1th time for the m print (' 1th times from%s meters high, walk through%s meters, and then rebound to% s meters. '% (h, a[0], H/2))) for I in Range (2,11):  #第1次已初始化, then Cycle 9 times    a.append (h)  #先计算路程, then height halved, because a ' bounce ' to 2    h = h/ 2    print ('%s ' was dropped from%s to a total of%s meters and then bounced back to%s m.) '% (i, H, sum (a), H/2))

The output results are as follows:

The 1th time from 100 meters high landing, walked 100 meters, and then rebounded to 50.0 meters.
The 2nd time from 50.0 meters high landing, a total of 200 meters, and then rebounded to 25.0 meters.
The 3rd time from 25.0 meters high landing, a total of 250.0 meters, and then rebounded to 12.5 meters.
The 4th time from 12.5 meters high landing, a total of 275.0 meters, and then rebounded to 6.25 meters.
The 5th time from 6.25 meters high landing, a total of 287.5 meters, and then rebounded to 3.125 meters.
The 6th time from 3.125 meters high landing, a total of 293.75 meters, and then rebounded to 1.5625 meters.
The 7th time from 1.5625 meters high landing, a total of 296.875 meters, and then rebounded to 0.78125 meters.
The 8th time from 0.78125 meters high landing, a total of 298.4375 meters, and then rebounded to 0.390625 meters.
The 9th time from 0.390625 meters high landing, a total of 299.21875 meters, and then rebounded to 0.1953125 meters.
The 10th time from 0.1953125 meters high landing, a total of 299.609375 meters, and then rebounded to 0.09765625 meters.

That is: the 10th time Landing, a total of 299.609375 meters, the 11th time rebound to 0.09765625 meters height.

++++++++++++++++++++++++++++++++++++++

Source: Programming Language Introduction Classic 100 Cases "Python Edition"

Python exercises 015: A free-standing ball

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.