/* 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)}}