Kotlin algorithm Introduction to free falling algorithm

Source: Internet
Author: User
/* A ball from the height of 100 meters of freedom falling, each landing after jumping back to the original height of half, and then fall, to ask for it on the 10th landing, the total number of meters. The 10th rebound how high here with BigDecimal to avoid the loss of precision * *classFreefallingbody { FunTradition () {//traditional algorithm loop 10 calculations here with BigDecimal to avoid loss of precisionValDistance = BigDecimal (100.0)varHeight = BigDecimal (100.0)varResult:bigdecimal? =NULLValPercentage = BigDecimal (2.0) for(Iinch0..9) {height = height.divide (percentage)if(Result = =NULL) result = Distance.add (height)Else result = Result.add (height)//system.out.println ("distance" + result); } println (Result)}/** * Here with the BigDecimal to avoid the loss of precision * with 1 as the number of times the beginning of the forward recursion to the nth time after the free fall after the small ball movement of how much distance * custom for Nth times since How much distance the ball moves from the body after the fall, and how much distance the ball moves after the nth time between the inverse of the K-th times. * Recursive inverse motion distance distance the first movement distance is 0 */ FunDiyfreefallingbody (Fallingtimes:int, Thistimes:int, Height:bigdecimal, percentageheight:bigdecimal, Distance: BigDecimal): BigDecimal {varDistance = distance/** The same number of falling times will return directly to the height */if(Thistimes = = fallingtimes)returnDistance.add (Height.divide (percentageheight))else if(Thistimes < Fallingtimes) {/** The next free-fall height with repeated forward recursion */if(Thistimes = = 1) distance = heightValNextheight = Height.divide (percentageheight)returnDiyfreefallingbody (Fallingtimes, Thistimes + 1, nextheight, Percentageheight, Distance.add (nextHeight))}else if(Thistimes > fallingtimes + 1) {/** repeated reverse recursion for the next free fall Height */ValLastheight = height.multiply (percentageheight)if(Distance.toint () = = 0) distance = height println ("Distance$Distance")returnDiyfreefallingbody (Fallingtimes, ThisTimes-1, Lastheight, Percentageheight, Distance.add (lastheight) )
        }else if(Thistimes = = fallingtimes + 1) {returnDistankotlince.add (Height.multiply (percentageheight))}returnBigDecimal (0)}}

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.