Fibonacci sequence-rabbit reproduction problem

Fibonacci sequence-rabbit reproduction problem

The Fibonacci sequence was introduced because of the mathematician Leonardo Fibonacci's use of rabbit reproduction as an example, so it is also called the "rabbit sequence".

Generally speaking, a rabbit has the ability to reproduce after two months of birth, and a pair of rabbits can give birth to a pair of little rabbits every month. If all rabbits are not dead, how many pairs of rabbits can be bred in one year?

We might as well analyze a pair of newly born rabbits:

In the first month, the little rabbits did not have the ability to reproduce, so they were still a pair.
Two months later, there were two pairs of
rabbits. Three months later, the old rabbit gave birth to another pair, because the little rabbits had no ability to reproduce. , so a total of three pairs
------
pups = number of the previous month to the number of rabbits
into rabbits for a few months before = + log into rabbit pups to the previous month the number of
total number of rabbits = month into this number + Logarithm

It can be seen that the number of cubs, the number of adult rabbits, and the total number of rabbits all constitute a series of numbers. The very obvious feature of this sequence is that the sum of the two adjacent items in the front constitutes the latter item.

By analogy, the following chart can be listed: The
image description
Fibonacci number sequence is 1, 1, 2, 3, 5, 8, 13, 21, 34... This sequence starts from the third item, and each item is equal to the first two items. Sum, the recurrence formula is F(n)=F(n-1)+F(n-2), n≥3, F(1)=1, F(2)=1.
The following is a recursive method to achieve rabbit reproduction:

#include<stdio.h>
int Fib(int n)
{
	if (n <=2)
	{
		return 1;
	}
	else
	{
		return Fib(n - 1) + Fib(n - 2);//每一项都等于前两项之和
	}
}
int main()
{
	int n;
	scanf("%d", &n);
	printf("%d\n", Fib(n));
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45796387/article/details/111207146