A fool-type understanding recursive PHP recursion

Source: Internet
Author: User

  Write a program so long, sometimes others will ask some algorithms such as sorting ah, recursive Ah, always do not know how to say, today to tidy up, let more people to fool-type understanding recursion. Recursion has a lot of definitions on the web, but there is one sentence to listen to the most: recursion is to call yourself! Quote a story from the encyclopedia to understand:

Once there was a mountain, there is a temple in the mountain, there is an old monk in the temple, is telling a story to the Little monk! What is the story? "There used to be a mountain, a temple in the mountains, an old monk in the temple, telling a story to the Little monk!" What is the story? There used to be a mountain, a temple in the mountains, an old monk in the Temple, and a story for the Little Monk. What is the story? ......’”

Such a story is not doing a lot of repetitive things, like this can use recursion, recursion requires a few conditions:

1, recursion must have boundary conditions, that is, recursive exit (exit recursion)

2, recursive forward segment and recursive return segment, that is, the last obtained value

3, when the boundary condition (recursive exit) is not satisfied, recursion advances; When the boundary condition (recursive exit) is satisfied, the recursion returns.

Give a small recursive example, for example,

Zhang San go and John Doe borrow money, John Doe said you wait, I went to find Harry lent you,

Then John Doe to find Harry borrow money, Harry said you wait, I went to find Zhao Liu lent you,

Finally Harry find Zhao Liu borrow money, Zhao Liu lent Harry. (This is the recursive exit)

Here is a rule that is the first to perform the final return, such as Zhang San to borrow money, the end will give him.

A fool's understanding of recursion is to forget recursion, assuming that its sub-problem has been solved, from the above example is the assumption that John Doe has money.

Here's a scenario, Fibonacci sequence:1, 1, 2, 3, 5, 8, +,144 ,------

The and of the first two items are known, and the third one, because the method is reused, so the recursive solution is used:

The law of the Fibonacci sequence is the sum of the current term equals the first two, and the resulting formula is F (n) =f (n-1) +f (n-2); here n represents the first few items, the upper recursive code:

This assumes that the sub-problem has been resolved, adding the value formula for the 10th item is: F (Ten) =f (9) +f (8) So the above formula appears

?
1 2 3 4 5 6 function f($n){     if($n==1 || $n==2){         return 1; // 递归出口,     }     return f($n-1)+f($n-2); // 假设子问题已经解决 }

Use the iterative method to find the Fibonacci sequence:

?
1 2 3 4 5 6 7 8 9 10 11 function f($n){     $a = 1;     $b = 1;      $v = 1;<br>     for($i=3;$i<=$n;++$i){         $v = $a+$b;         $b = $a;         $a = $v;     }     return $v; }

We often encounter the interview problem: a monkey look at a bunch of peaches, every day, eat half, and eat one more! When the tenth day, there was only one peach. Question, how many peaches?

Day, only a peach, the Nineth Day is (+) * 2 peaches, assuming the sub-problem has been resolved, f (1) = (f (2) +1) * *, the first day of the peach is equal to the next day peach plus 1 times 2, on the code:

?
1 2 3 4 5 6 7 8 9 /**  * 递归实现方法  */ function f($n=1){     if($n==10){         return 1; // 递归出口     }     return (f($n+1)+1)*2; }

?
1 2 3 4 5 6 7 8 9 10 11 12 /**  * 用迭代的方法  */ function f($n){    $v = 1; // 设定初始值     for($i=9;$i>=$n;--$i){         $v = ($v+1)*2;     }     return $v; } echo f(1);

Then we will add some examples of recursion, to be continued, if there is anything unclear, please criticize me

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.