Classic algorithm 40 Cases: 1

Source: Internet
Author: User

Tags: oid jpg color stat div http thinking width sys

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

Related Keywords:
Related Article