Based on the details of JS recursive functions and practical examples (recommended), js Recursion

Source: Internet
Author: User

Based on the details of JS recursive functions and practical examples (recommended), js Recursion

The Programming Technique of program calling itself is called recursion ).

A process or function directly or indirectly calls itself in its definition or description, it usually converts a large and complex problem into a small problem similar to the original problem to solve it, the recursive strategy can describe the repeated computing required for the problem-solving process with only a small number of programs, greatly reducing the amount of code in the program. The ability to recursion lies in the use of limited statements to define an infinite set of objects. Programs Written with recursive thinking are often very simple and easy to understand.

In general, recursion requires boundary conditions, recursive forward segments, and recursive return segments. If the boundary condition is not met, recursive advances. If the boundary condition is met, recursive returns.

Note:

(1) recursion is to call itself in a process or function;

(2) When using an incremental strategy, there must be a clear recursive end condition, called a recursive exit, otherwise it will be infinitely sustained (deadlock ).

Recursive Algorithms are generally used to solve three types of problems:

(1) data is defined recursively. (Fibonacci function)

(2) The problem solution is implemented by recursive algorithms. (Backtracking)

(3) The data structure is defined by recursion. (Tree traversal, Graph Search)

Disadvantages of recursion:

Recursive Algorithms are less efficient in solving problems. During the recursive call process, the system opens a stack for storing the return point and local volume of each layer. Too many recursion times may cause stack overflow.

Examples of recursive functions:

1. Classical problems-a rabbit was born every month from the first 3rd months after birth. After the third month, a rabbit was born every month. If the rabbit did not die, q: What is the total number of rabbits per month in the third year? (Note: The rabbit rule is a sequence of numbers 1, 1, 2, 3, 5, 8, 13, 21 ....)

 class Program {  static void Main(string[] args)  {   Program p = new Program();   Console.WriteLine(p.tuzi(7));  }  public int tuzi(int n)   {   if (n == 1 || n == 2)   {    return 1;   }   else    {    return tuzi(n - 1) + tuzi(n - 2);   }  } }

2. Interesting question-age. Five people sat together and asked how old is the fifth person? He said he is two years older than 4th. Asked about the age of 4th People, he said he was two years older than 3rd people. Ask the third person, and say that the person is two years older than 2nd. Ask 2nd people, saying they are two years older than the first. Finally, I asked the first person that he was 10 years old. How old is the fifth person? Use recursive algorithms.

Class Program {static void Main (string [] args) {Program p = new Program (); Console. writeLine (p. age (5 ));} /// <summary> /// calculate the age by recursion /// </summary> /// <param name = "n"> several people </param> // /<returns> </returns> int age (int n) {int c; if (n = 1) return 10; else {c = age (n-1) + 2; return c ;}}

3. Interesting questions-monkeys eat peaches. There are a bunch of peaches on the beach, and five monkeys can score points. The first monkey divided the pile of peach creden into five portions, one more. The monkey threw the other one into the sea and took one. The second monkey divides the remaining peaches into five equal portions and adds one more portion. It also throws one more portion into the sea and takes one portion, the third, fourth, and fifth monkeys did this. How many peaches did they have on the beach?
Code:

Class Program {static void Main (string [] args) {Program p = new Program (); Console. writeLine (p. peachNumber (5 ));} /// <summary> /// calculate the number of peaches by recursion /// </summary> /// <param name = "n"> </param> // <returns> </returns> int PeachNumber (int n) {if (n = 1) {// The last one is at least six return 6;} else {return (PeachNumber (n-1) + 1) * 5 ;}}

The above details and practical examples (recommended) based on the JS recursive functions are all the content shared by Alibaba Cloud xiaobian. I hope you can give us a reference and support for our customer base.

Related Article

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.