Understanding of JS recursive function refinement and practical examples

Source: Internet
Author: User

The programming technique called by the program itself is called recursion (recursion).
A process or function in its definition or description of the direct or indirect call itself a method, it usually transforms a large complex problem layer into a small problem similar to the original problem to solve, the recursive strategy requires a small number of programs to describe the process of solving the problem of repeated calculations, greatly reducing the program The amount of code . The ability to recursion is to define an infinite set of objects with limited statements. The program written with recursive thought is often very simple and easy to understand.
In general, recursion requires boundary conditions, recursive forward segments, and recursive return segments. Recursion advances when boundary conditions are not met, and returns recursively when the boundary conditions are met.
Attention:
(1) Recursion is the invocation of itself in a process or function;
(2) When using the incremental return strategy, there must be a definite recursive end condition called recursive egress, otherwise it will go indefinitely ( deadlock ).

Recursive algorithms are generally used to solve three types of problems:
(1) The definition of the data is defined by recursion. (Fibonacci function )
(2) The problem solution is implemented by recursive algorithm. ( backtracking )
(3) The structural form of the data is defined recursively. ( Traversal of the tree, search of the graph )

Disadvantages of recursion:
Recursive algorithm is less efficient in solving problems. In the process of recursive invocation, the system opens up a stack for each layer's return point, local quantity and so on. Too many recursion times can cause stack overflow and so on.

Interesting examples of recursive functions:

1, Classical problems-a pair of rabbits, from the first 3 months after birth a pair of rabbits each month, the rabbit after the third month after the birth of a pair of rabbits, if the rabbit is not dead, ask the third year the total number of rabbits per month? (Hint: The law of the Rabbit for the series 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. There are 5 people sitting together and asking how old is the five? He said he was 2 years older than the first 4 . Asked the age of 4, he said he was 2 years older than the first 3 people . Ask the third person, and say that 2 people are two years older than the first. Ask the 2 person, say two years old than the first one. Finally asked the first person, he said is 10 years old. How old is the fifth one? It is implemented by recursive algorithm.

Class program
    {
        static void Main (string[] args)
        {
             Program P = new program ();
          
             Console.WriteLine (P.age (5));
       }

       //<summary>
       / Recursive method for Age
       //</summary>
        //<param name= "n" > a few 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 question--monkey eat peach. There are a bunch of peaches on the beach and five monkeys. The first monkey divided this pile of peaches into five copies, one more, and the monkey threw one more into the sea and took a copy. The second monkey to the rest of the peach evenly divided into five parts, and one more, it also threw a lot of one into the sea, took a copy, the third, the fifth monkey is doing this, ask the beach at least how many peaches?

Code:

Class Program
{
static void Main (string[] args)
{

Program P = new program ();

Console.WriteLine (P.peachnumber (5));

}
<summary>
How to find 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;
}
}

Understanding of JS recursive function refinement and practical examples

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.