[In-depth analysis of C language] In-depth understanding of the recursive algorithm of functions in C language

definition

Recursion: A function calls itself within its body . This calling process is called recursion. Such a function is called a recursive function.

In a recursive call, the calling function is also the called function. Executing a recursive function will call itself repeatedly, entering a new layer with each call

difficulty

Running a recursive function will call itself endlessly, which is of course incorrect!

In order to prevent the recursive call from proceeding without termination , there must be a conditional judgment statement to terminate the recursion in the function. After a certain condition is met, the recursive call will not be made, and then return layer by layer ! ! !

This is also a difficulty that recursion is not easy to understand!

for example

I'll use an example to illustrate how this recursion is used.

Example 1

Takes an integer value (unsigned) and prints its bits in order.
For example:
input: 1234, output 1 2 3 4

code show as below:

#include <stdio.h>

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

int main()
{
    
    
    unsigned int num = 0;
    scanf("%u", &num);
    
    Print(num);

    return 0;
}

analyze:

I have drawn a picture here, with the following analysis, as shown in the figure: (red means handover, green means return)
insert image description here

When we go to the main function, we first enter a number: 123, thennum = 123

Then we call our Print(num)function, at which point we call it hand out

Figure A:

  • So in Figure A at this time n = 123, then judge the if statement: 123 > 9true, enter the interior, and then call the Print(n / 10)function

  • The Print function called this time goes to Figure B. At this time, what we pass in is 123 / 10the result of: , which is 12

Figure B:

  • So in Figure B n = 12, then judge the if statement: 12 > 9true, enter the interior, then call the Print(n / 10)function again

  • The function of Print called this time goes to Figure C. At this time, what we pass in is 12 / 10the result of: , which is 1

Figure C:

  • So in Figure C n = 1, then judge the if statement: 1 > 9false, so execute the print statement directly, , then 1 and a space1 % 10 = 1 will be printed on the screen

Figure B:

  • At this point, the function in Figure C has been called, then we must return to the function in Figure B, execute the printfstatementn = 12
    in Figure B, and then in Figure B 12 % 10 = 2, then print 2 and a space

Figure A:

  • At this point, we have to return from Figure B to Figure A, execute the printfstatement in Figure A, and in Figure A n = 123, then 123 % 10 = 3, then print 3 and a space

Main function:

  • After the function of Figure A is executed, we have to return to our main functionPrint(num) . At this time, our function calling task has ended, and the screen prints the output:1 2 3

This is the recursive process, hand out, return,

Example 2

Let's do another test to test it!

There are 5 students together
Ask how old is the fifth student? He said he was 2 years older than the fourth student;
how old was the fourth student? He said he was 2 years older than the third student;
how old was the third student? He said he was 2 years older than the second student;
how old was the second student? He said he was 2 years older than the first student;
finally asked the first student and he said it was 10 years old.
How old is the fifth student?

Here, we use recursive thinking to do this problem

think

age(5) = age(4) + 2;
age(4) = age(3) + 2;
age(3) = age(2) + 2;
age(2) = age(1) + 2;
age(1) = 10;

Mathematically expressed as follows:

    age(n)= 10; (n = 1)

    age(n)= age(n-1) + 2;(n > 1)

It can be seen that n >1at that time , the formula for finding the age of the nth student was the same

Therefore, the above relationship can be represented by a function. The figure shows the process of finding the age of the fifth student
insert image description here

Obviously, this is a recursion problem

As shown in the figure, the solution can be divided into two stages:

Stage 1 is backtracking , that is, expressing the nth student's age as (n-1)a function of the (n-1)th student whose age is still unknown, and backtracking to the (n-2)th student's age...until the first student's age. At this point age(1) is known and there is no need to push it forward.

Then start the second stage, using a recursive method, calculate the age of the second student (12 years old) from the known age of the first student, and calculate the age of the third student (14 years old) from the age of the second student ) ... until the age of the fifth student (18 years old) is calculated.

That is to say, a recursive problem can be divided into two stages : backtracking and recursion . It takes several steps to find the final value. Obviously, if the recursive process is not required to proceed indefinitely, there must be a condition to end the recursive process.

In this example, age(1)=10, which is the condition to end the recursion.

code show as below:

#include <stdio.h>

int age(int n)
{
    
    
    int c = 0;
    if (n == 1)
    {
    
    
        c = 10;
    }
    else
    {
    
    
        c = age(n - 1) + 2;
    }
    return c;
}
int main()
{
    
    

    int stu = 0;
    stu = age(5);
    printf("第五个学生的年龄为: %d\n", stu);
    return 0;
}

Summarize

A procedure or function in its definition or specification has a way of calling itself directly or indirectly.
It usually transforms a large and complex problem layer by layer into a smaller problem similar to the original problem. Solving the
recursive strategy requires only a small amount of The program can describe the repeated calculations required for the problem-solving process, which greatly reduces the code amount of the program.

The main way of thinking about recursion is: make big things small

Guess you like

Origin blog.csdn.net/m0_63325890/article/details/121122267