Recursion and iteration of functions (analytical and graphical)

content

What is recursion?

The three elements of recursion

1. Make it clear what you want to do with this function

2. Find the end condition of the recursion

3. Find the equivalence relation of the function

 Takes an unsigned integer value and prints its bits in order.

Find the nth Fibonacci number. (regardless of overflow)


What is recursion?

The programming trick of a program calling itself is called recursion.
Transform a large and complex problem into a smaller problem similar to the original problem to solve.
The main way of thinking about recursion is to make big things small

Two necessary conditions for recursion

There is a constraint, when this constraint is met, the recursion does not continue.
It gets closer and closer to this limit after each recursive call.

The three elements of recursion

1. Make it clear what you want to do with this function

For recursion, I think a very important thing is what is the function of this function and what kind of thing is to be done, and this is completely defined by you. Such as:

// 求 n 的阶乘(n不为0)
int fun(int n)
{
    
}

2. Find the end condition of the recursion

The so-called recursion is to call the function itself within the function, so we must find out the end condition of the recursion, otherwise it will be an infinite loop.

int fun(int n)
{
    if(n == 1)
    {
        return 1;
    }
}

3. Find the equivalence relation of the function

We need to keep narrowing the scope of the parameters. After narrowing, we can make the result of the original function unchanged through some auxiliary variables or operations, and make big things small.

int fun(int n)
{
    if(n == 1)
    {
        return 1;
    }
    return fun(n-1) * n;
}

 Takes an unsigned integer value and prints its bits in order.

void print(int n)
{
	if (n > 9)
	{
		print(n / 10);
	}
	
	printf("%d ", n % 10);
	
}

int main()
{
	unsigned int num = 0;
	scanf("%d", &num);
	print(num);
	return 0;
}



Find the nth Fibonacci number. (regardless of overflow)

#include <stdio.h>
int Fib(n)
{
	if (n <= 2)
	{
		return 1;
	}
	else
	{
		return n + Fib(n - 1);
	}
}

int main()
{
	int num = 0;
	scanf("%d", &num);
	int ret = Fib(num);
	printf("%d\n", ret);
	return 0;
}

 

 

When we calculate Fib(7), Fib(3) has been repeatedly calculated 5 times. If we calculate a very large Fibonacci number, the underlying calculation amount is also very large, and the calculation time will be more than ten minutes. Or a few hours.
The stack space allocated to the program by the system is limited, but it is possible that you keep opening up the stack space, and eventually the stack space is exhausted. This phenomenon is called stack overflow.
How to solve the above problem:
rewrite recursion into non-recursion, that is, loop.

// 求 n 的阶乘
int fun(int n)
{
	int tmp = 1;
	while (n > 1)
	{
		tmp *= n;
		n --;
	}
	return tmp;
}

This way of writing, there will be no situation where a number is repeated many times.

Note:

Many times the solution is recursive, simply because it is simpler than non-recursive code.
When the data of a problem is quite large, it is very unreasonable to use recursion to implement it. At this time, the use of loop implementation can compensate for its problem.

Recursion is only used in some special cases.

If you have studied data structure, you will know the structure of stack. The recursion of functions is originally a kind of stack, and it is also first-in, last-out.

It takes a lot of practice to master recursion of functions.

Guess you like

Origin blog.csdn.net/qq_54880517/article/details/123697773