The essence of recursion is to return the result after zero vertices exist. If there is no zero point, an infinite loop will occur. Therefore, how to judge whether zero point is an important point for the successful use of recursion is provided below.
The essence of recursion is to return the result after zero vertices exist. If there is no zero point, an infinite loop will occur. So how to judge whether zero point is a key point for the successful use of recursion? the examples I have given below clearly comment on the zero point. Secondly, we need to understand the recursive process trend. In my summary, the following code goes into depth and then pops up. we can calculate the number of times the function has been executed. The following is an example of this problem.
Flat view printing?
01 |
// Calculate the factorial of N by recursive traversal of a simple array |
03 |
If (strtolower ($ _ SERVER ['request _ method']) = 'post ') |
05 |
$ Num = intval ($ _ REQUEST ['Number']); |
08 |
Exit ('Array is invalid! '); |
16 |
$ Result = recursive ($ num ); |
18 |
Echostrval ($ num). "the factorial is:". $ result; |
22 |
Echo "enter a number in the text box above! "; |
24 |
Functionrecursive ($ n) |
26 |
If ($ n = 1) return $ n; // The zero point is displayed. |
27 |
Return $ n * recursive ($ n-1); // here it is in-depth |
29 |
// The zero point of the factorial is a process of n-> 1-> n. |
35 |
// Recursive infinite classification of arrays |
36 |
$ Arr = array ('id' => 1, 'name' => 'News Center', 'Father '=> 0, 'Deep' => 1 ), |
37 |
Array ('id' => 2, 'name' => 'domestic Center', 'Father '=> 1, 'Deep' => 2 ), |
38 |
Array ('id' => 3, 'name' => 'Entertainment News ', 'Father' => 0, 'Deep '=> 1 ), |
39 |
Array ('id' => 4, 'name' => 'Sports News ', 'Father' => 3, 'Deep '=> 2 ), |
40 |
Array ('id' => 5, 'name' => 'start', 'Father '=> 4, 'Deep' => 3 ), |
41 |
Array ('id' => 6, 'name' => 'Beijing News ', 'Father' => 1, 'Deep '=> 2 ), |
42 |
Array ('id' => 7, 'name' => 'Beijing', 'Father '=> 3, 'Deep' => 2 )); |
45 |
// Father => parent ID |
46 |
// Deep => topic depth |
47 |
FunctionUnlimitedclass ($ arr = array (), $ fid = 0) |
49 |
// Static array must be used for array implementation; otherwise, the value will be lost. |
50 |
Static $ results = array (); |
51 |
If (is_array ($ arr) & $ arr) |
53 |
Foreach ($ arras $ k => $ v) |
55 |
// The zero point is the last node of all top-level columns. |
56 |
If ($ v ['Father '] = $ fid) |
58 |
$ Results [$ v ['id'] = array ('id' => $ v ['id'], 'name' => $ v ['name'], 'deep '=> $ v ['deep']); |
59 |
// Recursive call here is in-depth |
60 |
Unlimitedclass ($ arr, $ v ['id']); |
67 |
$ Re = Unlimitedclass ($ arr ); |
68 |
If (is_array ($ re) & $ re) |
70 |
Foreach ($ reas $ k => $ v) |
72 |
Echostr_repeat ('', $ v ['deep ']). $ v ['name'].' |
76 |
// A static variable is defined in the above example. The so-called static variable is that the value will not be lost after leaving the function; |
77 |
// The Monkey King election is also a classic array operation |
78 |
// Give you a number of N. remove a monkey every M times and keep repeating until the last one is left. then the monkey is the king. |
79 |
$ Monkeys = range (1, 10); // monkey ID |
80 |
$ J = 3; // remove one for every 3 |
81 |
For ($ I = 0; count ($ monkeys)> 1; $ I ++) |
83 |
If ($ I + 1) % $ j = 0) |
85 |
Unset ($ monkeys [$ I]); |
89 |
Array_push ($ monkeys, $ monkeys [$ I]); |
90 |
Unset ($ monkeys [$ I]); |