当前位置:网站首页>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 } } }
边栏推荐
- excel 透视表 值显示内容 不显示计数
- 兼容并蓄广纳百川,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang复合容器类型的声明和使用EP04
- The growth path of a 40W test engineer with an annual salary, which stage are you in?
- 3.1-Classification-probabilistic generative model
- [C语言] sscanf如何实现sscanf_s?
- 进阶-指针
- 2022 China Soft Drink Market Insights
- kali渗透测试环境搭建
- 2022-08-10 mysql/stonedb-slow SQL-Q16-time-consuming tracking
- 关于#sql#的问题:怎么将下面的数据按逗号分隔成多行,以列的形式展示出来
猜你喜欢
随机推荐
The most complete documentation on Excel's implementation of grouped summation
1.1-Regression
1106 2019 Sequence (15 points)
Swagger简单使用
C Primer Plus(6) 中文版 第1章 初识C语言 1.7 使用C语言的7个步骤
Keep track of your monthly income and expenses through bookkeeping
Write a resume like this, easy to get the interviewer
CSDN21天学习挑战赛——封装(06)
Break pad source code compilation--refer to the summary of the big blogger
oracle数据库中列转行,列会有变化
Pico neo3 Unity Packaging Settings
笔试题大疆08.07
Square, multi-power, square root calculation in Tf
Machine Learning Summary (2)
记录一些遇见的bug——Lombok和Mapstruct的冲突导致,A component required a bean of type ‘com.XXX.controller.converter.
XXL-JOB 分布式任务调度中心搭建
4.1 - Support Vector Machines
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
进阶-指针