[C Language-Recursion] Use recursion to print out 1234

The topics are as follows:

接受一个整型值(无符号),按照顺序打印它的每一位
例如:
输入:1234  输出:1 2 3 4

The question requires the use of recursion, so what does the idea of ​​recursion mean?

There is an example that illustrates this point well, look up in the dictionary:

I didn’t understand a noun A, so I looked it up in the dictionary. In the explanation of A, there was a noun B that I didn’t understand. So I looked up noun B. After looking up the explanation of noun B, there was a noun C in it. I didn’t understand, so I looked up the C noun again.

Now that I understand the C noun ( actually it is a recursive exit ), I understand the previous B noun. If B understands it, I finally understand the C noun, and the dictionary query ends.

The idea of ​​​​this question:

        When I wanted to use recursion, I thought of a function that continuously calls itself, so I drew a black box as shown in the picture to represent the call. However, the black box cannot be drawn infinitely. There must be an outlet for recursion, which is a judgment statement. , if the conditions are met, I will continue to call myself recursively. If the conditions are not met, I will not call myself and start returning.

 

        As shown in the figure, recursion is to divide a large problem into small problems similar to the original one. If I want to print 1234, then I will print 123. If I know how to print 123, I will print 12, and continue to reduce the size of the print. , and finally I print 1 just fine.

       The whole process is like a green line, but the last time I don't call myself, it means that I don't meet the calling conditions. How to find this condition?

Idea: Keep going into the function body, and finally decompose 1234 into only one number, which is 1. Obviously this only has one digit, so there is no need to peel it off layer by layer, so the condition can be set as: the size of the parameters passed in When it is less than 9, I will jump out of the recursion. If the parameter is greater than 9, it means that it has not been decomposed into only one digit, so continue to peel off.

So I wrote:

void print(int n)
{
	if (n > 9)      //跳出条件
	{
		
	}
	
	
}

      When the value passed in is >9, I will continue to call, so I will add a statement to call its own function in the if statement:

void print(int n)
{
	if (n > 9)      //必须:跳出条件
	{
		print( ? );  
	}
	
	
}

      Every time I call, I should continue to approach this recursive exit, that is, 1234--->1, that is, when I call, I have to continue to decompose 1234, so the parameters of the function become like this, which is reflected in the green picture above. What the line expresses:

void print(int n)
{
	if (n > 9)      //必须:跳出条件
	{
		print(n / 10);  //必须:逼近跳出条件
	}
	
	
}

The above code corresponds to the orange box in the picture above, including the escape condition and  the statements that approximate the escape condition in the call.

Back to the first diagram:

When I didn't meet the condition for the last time, the recursion ended, and the program bypassed the if() part of the statement body, so I should print out 1.

So below the judgment, add the statement to print 1 2 3 4

void print(int n)
{
	if (n > 9)      //必须:跳出条件
	{
		print(n / 10);  //必须:逼近跳出条件
	}
	
	printf("%d ", n % 10);
}

In this way, the recursive function is written. The most important thing is to understand the schematic diagram drawn above . (It took me a while to draw a picture. I am most satisfied with this picture -_-)

This is my first time learning. If you have any misunderstandings, please correct me!

Supongo que te gusta

Origin blog.csdn.net/ggbb_4/article/details/129156940
Recomendado
Clasificación