Java Recursive algorithm classic example (Classic rabbit problem) _java

Source: Internet
Author: User

Title: Classical Questions: 3 months from the month of a pair of rabbits, the rabbit long to the third month after the birth of a pair of rabbits each month, if the rabbit is not dead, ask the number of rabbits each month for how much?

Analysis: First of all, we need to understand the meaning of the topic refers to the total logarithm of the rabbit every month; if the rabbit is divided into small and middle three species, rabbits will produce a pair of rabbits every month after three months of birth,

Then we assume that the first month of the rabbit as a small rabbit, the second month for the Rabbit, the third month after the Big Rabbit, then the first month, there are 1, 0, 0, the second month are 0, 1, 0,

The third month were 1, 0, 1, fourth month respectively, 1, 1, 1, fifth month respectively 2, 1, 2, sixth month respectively 3, 2, 3, seventh month respectively 5, 3, 5 ...

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

So we have a rule that from the third month onwards, the total number of rabbits behind is equal to the total number of rabbits in the preceding two months, which is the Fibonacci sequence.

Java code:

A classic example of a recursive algorithm.

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring some help, but also hope that a lot of support cloud Habitat community!

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.