Classic example of Java recursive algorithm (classic rabbit problem)

Title: classical question: a pair of rabbits are born every month from three months. After the third month, a pair of rabbits are born every month. If the rabbits do not die, what is the total number of rabbits every month?

Analysis: first of all, we should understand that the title refers to the total logarithm of rabbits every month; Suppose that rabbits are divided into three types: small, medium and large, and rabbits will give birth to a pair of rabbits every month from three months after birth,

Then we assume that the rabbits in the first month are small rabbits, the second month is medium rabbits, and the third month is big rabbits. Then there are 1, 0 and 0 in the first month and 0, 1 and 0 in the second month,

The third month is 1, 0 and 1, the fourth month is 1, 1 and 1, the fifth month is 2, 1 and 2, the sixth month is 3, 2 and 3, and the seventh month is 5, 3 and 5

The total number of rabbits are: 1, 1, 2, 3, 5, 8, 13

Therefore, a rule is obtained that from the third month, the total number of rabbits in the later months is equal to the sum of the total number of rabbits in the previous two months, that is, the Fibonacci series.

Java code:

A classic example of a recursive algorithm.

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>