Java classic rabbit problem, java classic rabbit

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.