C # language basics-recursion,

Source: Internet
Author: User

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?

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.