当前位置:网站首页>Kotlin Algorithm Getting Started with Rabbit Number Optimization and Expansion
Kotlin Algorithm Getting Started with Rabbit Number Optimization and Expansion
2022-08-11 08:13:00 【Yi Pangzhou】
/* 古典问题:3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 分析:首先我们要明白题目的意思指的是每个月的兔子总对数;假设将兔子分为小中大三种,兔子从出生后三个月后每个月就会生出一对兔子, 那么我们假定第一个月的兔子为小兔子,第二个月为中兔子,第三个月之后就为大兔子,那么第一个月分别有1、0、0,第二个月分别为0、1、0, 第三个月分别为1、0、1,第四个月分别为,1、1、1,第五个月分别为2、1、2,第六个月分别为3、2、3,第七个月分别为5、3、5…… 兔子总数分别为:1、1、2、3、5、8、13…… 于是得出了一个规律,从第三个月起,后面的兔子总数都等于前面两个月的兔子总数之和,即为斐波那契数列.*/ class RabbitNumber { private var rabbits: Long = 1 private var lastSecondRabbits: Long = 0 private var lastRabbits: Long = 0 /** * Traverse from the first month to the firstn个月的兔子总数 */ fun forEachMothsToRabbits(moths: Int) { println(System.currentTimeMillis()) for (i in 1..moths) println("第" + i + "个月兔子数为" + getRabbits(i)) println(System.currentTimeMillis()) } /** * Get the total number of rabbits for the current month */ private fun getRabbits(moths: Int): Long { if (moths == 1 || moths == 2) return rabbits = 1 else if (moths == 3) return rabbits = 2 else { //Initialize the number of rabbits in the last month and the last two months if (lastRabbits == 0L && lastSecondRabbits == 0L) { lastSecondRabbits = getRabbits(moths - 2) lastRabbits = getRabbits(moths - 1) } //Count the number of rabbits returned by this one rabbits = lastRabbits + lastSecondRabbits /*Let the number of rabbits in two months equal the number of rabbits in the previous month,Let last month equal the number of rabbits in this month 为了下一次计算(Number of rabbits next month)More efficient and faster to avoid redundant recursion affecting the calculation speed*/ lastSecondRabbits = lastRabbits lastRabbits = rabbits return rabbits } } }
边栏推荐
- The growth path of a 40W test engineer with an annual salary, which stage are you in?
- 欢迎加入sumarua网络安全交流社区
- Linux,Redis中IOException: 远程主机强迫关闭了一个现有的连接。解决方法
- Kotlin算法入门求回文数数算法优化二数字生成规则
- 《剑指offer》题解——week3(持续更新)
- 4.1ROS运行管理/launch文件
- JUC Concurrent Programming
- Square, multi-power, square root calculation in Tf
- 软件测试常用工具的用途及优缺点比较(详细)
- jenkins简单使用
猜你喜欢
The softmax function is used in TF;
几何EX3 功夫牛宣布停售,入门级纯电产品为何总成弃子
Active users of mobile banking grew rapidly in June, hitting a half-year high
1.2-误差来源
Pico neo3 Unity Packaging Settings
JRS303-Data Verification
Use tf.argmax in Tensorflow to return the index of the maximum value of the tensor along the specified dimension
Interaction of Pico neo3 in Unity
【云原生】云原生在网络安全领域的应用
Four operations in TF
随机推荐
tf.reduce_mean() and tf.reduce_sum()
Pico neo3 Unity Packaging Settings
Four states of Activity
迷你图书馆系统(对象+数组)
TF generates (feature, label) set through feature and label, tf.data.Dataset.from_tensor_slices
麒麟V10系统打包Qt免安装包程序
1061 True or False (15 points)
Kotlin算法入门求完全数
选择收银系统主要看哪些方面?
kali渗透测试环境搭建
Active users of mobile banking grew rapidly in June, hitting a half-year high
Swagger简单使用
用 Antlr 重构脚本解释器
【43. 字符串相乘】
通过记账,了解当月收支情况
笔试题大疆08.07
About # SQL problem: how to set the following data by commas into multiple lines, in the form of column display
[C语言] sscanf如何实现sscanf_s?
装饰器模式:Swift 实现
2.1-梯度下降