Java classic rabbit problem, java classic rabbit
Question: classical question: a rabbit has been born every month since 3 months. After the third month, a rabbit has been born every month. If the Rabbit does not die, how many rabbits are there every month?
Analysis: first, we need to understand the meaning of the question refers to the total number of rabbits each month. Suppose we divide rabbits into three types: small, medium, and large, A rabbit will have a rabbit every month three months after birth,
Then we assume that the rabbit in the first month is a rabbit, the rabbit in the second month is a rabbit, and the rabbit in the third month is a big rabbit. Then there are 1, 0, and 0 in the first month, respectively, the second month is 0, 1, 0,
The third month is 1, 0, 1, and the fourth month is 1, 1, and 1, and the fifth month is 2, 1, and 2, respectively, the sixth month is 3, 2, and 3, and the seventh month is 5, 3, and 5, respectively ......
The total number of Rabbits is: 1, 1, 2, 3, 5, 8, 13 ......
So we come up with a rule that from the third month, the total number of Rabbits after is equal to the sum of the total number of rabbits in the previous two months, that is, the Fibonacci series.
Java code:
A typical example of a recursive algorithm.