Chapter 7
Recursion
7.1 Basic Concepts
According to the process of the original book, we should now talk about recursion. Recursion is a powerful mathematical tool. I don't know whether you have learned Lisp or its dialects (such as scheme). If you have learned it, you will be very familiar with recursion, because in LISP and its dialects, there are no loop statements. To construct a loop, you must implement it in recursive form. At that time, my head couldn't bend, because I was used to using for, while, and other statements in C/C ++ to loop, at the beginning of lisp, I had almost no way to write a loop without errors.
For example, the following code:
For (INT I = 0; I <= 10; ++ I)
{
}
Can be converted to recursion:
Void recursion_loop (int I)
{
If (I = 10)
Return;
Else
Recursion_loop (I + 1 );
}
// Call:
Recursion_loop (0 );
Recursion has the following properties:
Recursion is to repeatedly call itself in a process. For example, in the above example, it is called in the recursion_loop () function.
You must have a condition for stopping. This is easy to understand, because if there is no condition for stopping, this recursion will lead to the infinite collapse of the Child sun and sun. For example, in the above example, if (I = 10) is the condition for stopping.
Recursion is limited by actual conditions, for example, failure due to insufficient stack size. This is because in the computer, the stack size is limited, and each recursive call to the function itself needs to save the returned address, parameters, and other information in the stack, after N recursion, the stack is likely to be full, resulting in the failure to perform the (n + 1) recursion.
Based on the above nature 3, we can know that not all languages Support recursion-if a language supports recursion, it must support the "stack" structure. What I know now is the most fascinating language for Recursive use. LISP and its dialects are well-deserved kings.
7.2 applications
Alas, we didn't want to write recursive examples because these examples have been written countless times. When it comes to recursion, we will certainly talk about three examples: factorial, Fibonacci series, and tower of Hanoi. But for the purpose of repeating the textbooks, I will repeat the work again (but I will not explain the three examples any more. I will find a book on the data structure ). Finally, add a Pascal triangle, which is also the famous Yang Hui triangle in China.
7.2.1 factorial
//////////////////////////////////////// ///////////////////////////////////////
//
// Filename: factorial. c
// Version 0.10
// Author: Luo Cong
// Date: 2005-1-8 21:23:16
// Comment:
//
//////////////////////////////////////// ///////////////////////////////////////
# Include <stdio. h>
Static long factorial (const long N)
{
Return 0 = n | 1 = n? 1: N * factorial (n-1 );
}
Int main ()
{
Long lresult = factorial (10 );
Printf ("% LD/N", lresult );
}
7.2.2 Fibonacci Series
//////////////////////////////////////// ///////////////////////////////////////
//
// Filename: fib. c
// Version 0.10
// Author: Luo Cong
// Date: 2005-1-8 21:28:56
// Comment:
//
//////////////////////////////////////// ///////////////////////////////////////
# Include <stdio. h>
Static long fib (const long N)
{
Return 0 = n | 1 = n? 1: fib (n-1) + fib (n-2 );
}
Int main ()
{
Long lresult = fib (10 );
Printf ("% LD/N", lresult );
}
7.2.3 tower of Hanoi
//////////////////////////////////////// ///////////////////////////////////////
//
// Filename: Hanoi. c
// Version 0.10
// Author: Luo Cong
// Date: 2005-1-8 21:40:44
// Comment:
//
//////////////////////////////////////// ///////////////////////////////////////
# Include <stdio. h>
Static void move (const char X, const int N, const char Z)
{
Printf ("Move disk % d from column % C to % C/N", n, x, z );
}
Static void Hanoi (const int N, const char X, const char y, const char Z)
{
If (1 = N)
Move (x, 1, Z); // if there is only one disk, move it directly from X to Z
Else
{
Hanoi (n-1, x, z, Y); // set 1 ~ Migrate n-1 disk from X to Y, and use Z as the intermediate
Move (x, N, Z); // move the nth disk from X to Z
Hanoi (n-1, Y, x, z); // set 1 ~ The N-1 disk is moved from Y to Z, and X is used as the intermediate
}
}
Int main ()
{
Hanoi (1, 'x', 'y', 'z ');
}
2.4 Pa SCA triangle (Yang Hui triangle)
The following value is called the Pascal triangle, which is a famous Yang Hui triangle in China:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
The number on the triangle boundary is 1, and each number inside is the sum of the two numbers located above it.
Using Recursion, we can easily convert the problem to this nature:
If F (row, col) indicates the col element of the row in the Yang Hui triangle, then:
F (row, col) = 1 (COL = 1 or ROW = col), that is, the recursive stop condition.
F (row, col) = f (Row-1, col-1) + f (Row-1, col), that is, the sum of two adjacent elements in the previous row.
With this nature, our recursive Programs are easy to write. Pai_^
//////////////////////////////////////// ///////////////////////////////////////
//
// Filename: pascaltriangle. c
// Version 0.10
// Author: Luo Cong
// Date: 2005-1-9 14:53:57
// Comment:
//
//////////////////////////////////////// ///////////////////////////////////////
# Include <stdio. h>
Static long getelement (const long row, const long col)
{
// The two peripheral elements of each row are 1
If (1 = col) | (ROW = col ))
Return 1;
Else
// The rest is the sum of the (Col-1) and (COL) elements of the previous row.
Return getelement (Row-1, col-1) + getelement (Row-1, col );
}
Static long pascaltriangle (const long N)
{
Int row;
Int Col;
For (ROW = 1; row <= N; ++ row)
{
For (COL = 1; Col <= row; ++ col)
Printf ("% 4ld", getelement (row, col ));
Printf ("/N ");
}
}
Int main ()
{
Pascaltriangle (5 );
}