Classic algorithm details (2): February Series
Note:
Fibonacci is a european mathematician in the 1200 s. In his book, he once mentioned that if one child is born every month, a child is also produced after one month. At first, there was only one free Sub-Account, two free sub-accounts in one month, and three free sub-accounts in two months, five free sub-accounts after three months (small sub-accounts are put into production )....... If you don't quite understand this example, you can see it in the figure below. Note that it takes only one month for a new child to be involved in production. A similar principle can also be used for plant growth, this is the Fibonacci series, which is generally referred to as the Fei's series, for example: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ......
Analysis:
If we look for the law of the February number in mathematics, it is easy to find that when n> = 2, f (n) = f (n-1) + f (n-2 ). But why is there a logic analysis rule? In fact, it is very simple because the number of rabbits in the nth month = the number of rabbits in the previous month + the number of new rabbits in this month. The number of rabbits in the previous month is f (n-1). What is the number of new rabbits in the previous month, because only rabbits that existed last month can be born this month, so the number of new rabbits is f (n-2 ).
Implementation:
/*************************************** * ************************* Name: gossipAlgorithm. c Description: the problem is a classical recursion problem Author: fuchencong@163.com Time: *************************************** * ************************/# include
Const int MAX_SIZE = 20; int main () {int arrayGossip [MAX_SIZE]; arrayGossip [0] = 0; /** the first element of array is 0 */arrayGossip [1] = 1; for (int I = 2; I
Summary:
The Fisher series can easily find out its laws in mathematics, but if you want to understand it logically, you have to think a little bit.