# A recursive Fibonacci sequence of C language data structures

• 2020-06-01 10:26:59
• OfStack

A recursive Fibonacci sequence of C language data structures

Because I am not familiar with recursion, it is very difficult to do POJ1753, which is to turn the pieces until all the pieces on the board color 1, to find the least number of times, the method is to enumerate recursion. Then I'm going to do another recursion problem (taking a combination of n elements out of an array), but again I don't understand the recursion problem very well. Ok, so we went over CPP, and on page 229, we saw the Fibonacci sequence, and we remembered the first problem we did, and we found that we could do it recursively. So I decided to optimize the previous 1.

The following is an excerpt from C primer plus

The Fibonacci sequence is defined as follows: the first and second digits are both 1, and each subsequent number is the sum of its first two digits. For example, the first digits in the sequence are 1, 1, 2, 3, 5, 8, and 13. ... Let's create a function that takes a positive integer n as an argument and returns the corresponding Fibonacci value.

First, recursion provides a simple definition of recursion depth. If Fibonacci() is called, Fibonacci(n) should return 1 when n is 1 or 2; For other values, return Fibonacci(n-1)+Fibonacci(n-2);

``````
long Fibonacci(n)
{
if (n > 2)
return Fibonacci(n-1)+Fibonacci(n-2);
else
return 1;
}
``````

Then there is the question of the total number of rabbits.

Have 1 pair of rabbit, from the 3rd month after birth every month gives birth to 1 pair of rabbit, small rabbit son grows to the 3rd month after giving birth again 1 pair of rabbit, if the rabbit dies not, every month how many is the logarithm of the rabbit?

When you think about it, if you just do a simple 1, you'll see that the log of the month of the rabbit is the Fibonacci sequence.

Month 1:1 yes;
Month 2:1 yes;
Month 3:2 pairs;
Month 4:3 pairs:
Month 5:5 pairs:
Month 6:8 pairs;
...

When I did this problem before, I thought it was a very simple idea, which is that if you start at month 3, and you figure out how many rabbits are in each month, you just add up the total for the first two months of the month.
This is my previous code, f1 and f2 for months. :

``````
#include<stdio.h>
int main()
{
int f1,f2;
int month,ct;
scanf("%d",&month);
if(month<=2)
printf(" Two of them. \n");
if (month > 2)
{
f1 = f2 = 1;
ct = 0;
while(ct < month -2){
f1 = f1+f2;
ct += 1;
f2 = f1+f2;
ct += 1;
}
if (month %2 == 0){
printf(" The first  %d  The logarithm of rabbits in months is: %d.\n",month,f2);
}
if (month %2 == 1){
printf(" The first  %d  The logarithm of rabbits in months is: %d.\n",month,f1);
}
}
return 0;
}
``````

In fact, this code is one step away from recursion, which is pretty close. But I had no idea.

This is the code I modified:

``````
#include<stdio.h>
long Fibonacci(n)
{
if (n > 2)
return Fibonacci(n-1)+Fibonacci(n-2);
else
return 1;
}
int main()
{
long num;
int month;
scanf("%d",&month);
num = Fibonacci(month);
printf(" The number of rabbits this month is zero %d.\n",num);
return 0;
}
``````

It was a simple change, but the code was much cleaner and easier to understand, and you learned new things.

To do a good job, workers must first sharpen their tools and encourage each other.