C # language basics-recursion,
Recursion
1. Concept conception:
The function itself is called in the body until it meets a certain condition and does not continue to be called.
2. conditions should be met. factor:
(1) There is a process of repeated execution (calling itself );
(2) Conditions for jumping out of the repeated execution process (function Exit)
Iii. example
Factorial calculation n! = N * (n-1) * (n-2) * (n-3 )*...... * 1 (n> 0)
// Int sum = I * jiecheng (I-1); parse
// I = 5
// Int sum = 5 * jiecheng (4 );
// Int sum = 5*(4 * jiecheng (3 ));
// Int sum = 5*(4*(3 * jiecheng (2 )));
// Int sum = 5*(4*(3*(2 * jiecheng (1 ))));
// Int sum = 5*(4*(3*(2*1 )));
Iv. Notes notice:
1. A condition for loop end must exist in recursion.
2. Each call to a recursive function requires stack storage. If the number of calls is too large, Stack Overflow may occur.
Example: 1. A group of sheep rushed to each village for sale. Every time a village was lost, the total number of 1/2 and 1 were lost. After 7 villages, there were two left. Then, I asked how many sheep were first evicted.
2. Write recursive functions for 2*4*6 *...... * (2n) Result
3. There is a pair of female and female rabbits. Every month, they can breed a pair of rabbits. How many rabbits are there after n months?