当前位置:网站首页>Kotlin算法入门求自由落体
Kotlin算法入门求自由落体
2022-08-11 08:01:00 【易庞宙】
/* 一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下, 求它在第10次落地时,共经过多少米?第10次反弹多高 这里用BigDecimal避免精度丢失 */ class FreeFallingBody { fun tradition() { //传统算法 循环10次计算这里用BigDecimal避免精度丢失 val distance = BigDecimal(100.0) var height = BigDecimal(100.0) var result: BigDecimal? = null val percentage = BigDecimal(2.0) for (i in 0..9) { height = height.divide(percentage) if (result == null) result = distance.add(height) else result = result.add(height) //System.out.println("distance" + result); } println(result) } /** * 这里用BigDecimal避免精度丢失 * 以1为次数起点正向递归求第n次后自由落体后小球运动了多少距离 * 自定义求第n次后自由落体后小球运动了多少距离 * 以及逆向求第k次前的第n次之间自由落体后小球运动了多少距离 * 递归逆向求运动距离时候distance第一次运动距离为 0 */ fun diyFreeFallingBody(fallingTimes: Int, thisTimes: Int, height: BigDecimal, percentageHeight: BigDecimal, distance: BigDecimal): BigDecimal { var distance = distance /**所求落体次数相同则直接返回高度 */ if (thisTimes == fallingTimes) return distance.add(height.divide(percentageHeight)) else if (thisTimes < fallingTimes) { /**反复正向递归求下一次自由落体高度 */ if (thisTimes == 1) distance = height val nextHeight = height.divide(percentageHeight) return diyFreeFallingBody(fallingTimes, thisTimes + 1, nextHeight, percentageHeight, distance.add(nextHeight)) } else if (thisTimes > fallingTimes + 1) { /**反复逆向递归求下一次自由落体高度 */ val lastHeight = height.multiply(percentageHeight) if (distance.toInt() == 0) distance = height println("distance$distance") return diyFreeFallingBody(fallingTimes, thisTimes - 1, lastHeight, percentageHeight, distance.add(lastHeight)) } else if (thisTimes == fallingTimes + 1) { return distanKotlince.add(height.multiply(percentageHeight)) } return BigDecimal(0) } }
边栏推荐
猜你喜欢
Keep track of your monthly income and expenses through bookkeeping
One-hot in TF
Find the latest staff salary and the last staff salary changes
1106 2019 Sequence (15 points)
3.1-分类-概率生成模型
1076 Wifi密码 (15 分)
Redis source code-String: Redis String command, Redis String storage principle, three encoding types of Redis string, Redis String SDS source code analysis, Redis String application scenarios
查找最新人员工资和上上次人员工资的变动情况
3.2 - classification - Logistic regression
Mysql JSON对象和JSON数组查询
随机推荐
1091 N-Defensive Number (15 points)
《剑指offer》题解——week3(持续更新)
leetcode: 69. Square root of x
Interaction of Pico neo3 in Unity
1106 2019 Sequence (15 points)
Swagger简单使用
1081 Check Password (15 points)
About # SQL problem: how to set the following data by commas into multiple lines, in the form of column display
Keep track of your monthly income and expenses through bookkeeping
1076 Wifi密码 (15 分)
TF generates (feature, label) set through feature and label, tf.data.Dataset.from_tensor_slices
剑指offer专项突击版第26天
Break pad source code compilation--refer to the summary of the big blogger
关于#sql#的问题:怎么将下面的数据按逗号分隔成多行,以列的形式展示出来
1101 How many times B is A (15 points)
Serverless + domain name can also build a personal blog? Really, and soon
Analysys and the Alliance of Small and Medium Banks jointly released the Hainan Digital Economy Index, so stay tuned!
1096 大美数 (15 分)
欢迎加入sumarua网络安全交流社区
opengauss创建用户权限问题