Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the Rabbit problem with java

Shulou Source: shulou.com Published: 2022-06-02 07:28:13 10月02日 Update

How to use java to solve the rabbit problem, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

Topic: classical question: there is a pair of rabbits who give birth to a pair of rabbits every month from the third month after birth, and the little rabbits grow up to give birth to another pair of rabbits every month after the third month. If the rabbits do not die, what is the total number of rabbits each month?

Answer:

Analysis: starting from the first pair of rabbits, one pair of rabbits in the first month, because "there is another pair of rabbits every month after growing to the third month", so the second month is also a pair of rabbits, and the third month can give birth to a pair of rabbits, so the total number is 2 pairs. At this time, one of them is an old rabbit who can give birth to a pair of rabbits every month, and the other is a pair of new rabbits. It can only give birth to a pair of rabbits a month in the third month. In that case, the total number of rabbits in the 4th month is 3 pairs. By analogy, the total number of rabbit pairs from the first month to the 12th month is as follows: 1magy 1pair2 pairs3, 5, 8, 13, 21, 34, 5, 89144.

This is a typical Fibonacci sequence.

Since each pair of new rabbits can only give birth to a new pair of rabbits every other month, the total number of rabbit pairs each month is the sum of the rabbit logarithms of the previous two months.

(method 1) it is basically realized by java programming, and the program design is as follows:

The code is implemented as follows:

Public class RabbitDemo {

Public static void main (String [] args) {

Int amount; / / the total number of rabbits per month

Int nAmount = 1; / / number of rabbits in the first month

Int oAmount = 1; / / number of rabbits in the second month

For (int iTunes 1)

Tags: Rabbit question total method three solution code sequence quantity more analysis help easy and so on simple and easy so that two sum typical content Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Microsoft Shulou Information vpn Shulou Tech Info