Classic algorithm 40 Cases: 1

Source: Internet
Author: User

1. There are a pair of rabbits, from the 3rd month after the birth of a pair of rabbits every month, the rabbit after the 4th month, a month, a pair of rabbits,

If the rabbit does not die, ask the total number of rabbits each month?

Problem-Solving ideas: First calculate a few months of the total logarithm of the rabbit, into the law of mathematics to find,

There is a condition of attention in the topic, the newborn bunny has a fertility capacity of one months long.

: 1 means 1 pairs of rabbits with fertility, ① a pair of rabbits with no fertility

  

It is known by law that the 7th month equals the 6th month plus the 5th month. Recursion can be used.

  

 Packagecom.qd.math40;/*** Created by Chenlongbo on 2017/6/3.*/ Public classRabbits { Public Static voidMain (string[] args) { for(inti = 1; I <= 10; i++) {System.out.println ("+i+" month, the logarithm of the rabbit is: "+f (i)); }    }     Public Static intFintN) {        if(n = = 1 | | n = = 2)            return1; Else        returnF (n-1) + f (n-2); }}

Program Run Solution:

The 1th month, the logarithm of the rabbit is: 1
The 2nd month, the logarithm of the rabbit is: 1
The 3rd month, the logarithm of the rabbit is: 2
The 4th month, the logarithm of the rabbit is: 3
The 5th month, the logarithm of the rabbit is: 5
The 6th month, the logarithm of the rabbit is: 8
The 7th month, the logarithm of the rabbit is: 13
The 8th month, the logarithm of the rabbit is: 21
The 9th month, the logarithm of the rabbit is: 34
The 10th month, the logarithm of the rabbit is: 55

  

Classic algorithm 40 Cases: 1

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.