当前位置:网站首页>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) } }
边栏推荐
- Serverless + domain name can also build a personal blog? Really, and soon
- 欢迎加入sumarua网络安全交流社区
- C Primer Plus(6) 中文版 第1章 初识C语言 1.6 语言标准
- 2.1-梯度下降
- 测试用例很难?有手就行
- My creative anniversary丨Thank you for being with you for these 365 days, not forgetting the original intention, and each is wonderful
- CIKM 2022 AnalytiCup Competition: Federal Heterogeneous Task Learning
- Creo9.0 特征的成组
- JUC并发编程
- 1003 I want to pass (20 points)
猜你喜欢

About # SQL problem: how to set the following data by commas into multiple lines, in the form of column display

装饰器模式:Swift 实现

项目1-PM2.5预测

9、Neural Sparse Voxel Fields

1051 Multiplication of Complex Numbers (15 points)

JUC并发编程

机器学习(二)线性回归

2022 China Soft Drink Market Insights

软件测试常用工具的用途及优缺点比较(详细)

1036 Programming with Obama (15 points)
随机推荐
兼容并蓄广纳百川,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang复合容器类型的声明和使用EP04
零基础SQL教程: 主键、外键和索引 04
oracle19c does not support real-time synchronization parameters, do you guys have any good solutions?
麒麟V10系统打包Qt免安装包程序
【415. 字符串相加】
The softmax function is used in TF;
RestTemplate工具类
Serverless + domain name can also build a personal blog? Really, and soon
Four startup modes of Activity
Two state forms of Service
1096 big beautiful numbers (15 points)
零基础SQL教程: 基础查询 05
1.2 - error sources
Analysys and the Alliance of Small and Medium Banks jointly released the Hainan Digital Economy Index, so stay tuned!
1091 N-Defensive Number (15 points)
oracle数据库中列转行,列会有变化
go-grpc TSL authentication solution transport: authentication handshake failed: x509 certificate relies on ... ...
欢迎加入sumarua网络安全交流社区
测试用例很难?有手就行
Mysql JSON对象和JSON数组查询