There are two types of human beings: men and women; algorithms: recursive and iterative/cyclic; recursion refersDefine yourself in a simple scenario.
In mathematics and computer science, recursion isIdeas and strategies, Can be usedDefinition of terms (what is an expression),Problem descriptionAndProblem Solving. Recursion used for solving problems is called recursion.
There is a story. Physicist computing 10! It will say, "Look, it is equal to 1*2 *~ * 10, that is, 3628800. The mathematician said, "Oh, the factorial of 10. It is equal to 10 multiplied by 9 !".
Recursive algorithm"RashlyI think that my simple situation is known. Since fact (n-1) is known, fact (n) can be obtained. This way"Rashness" is critical to understanding recursive concepts. The recursive method does not directly solve the problem, but changes the problem into a recursive exit problem. A recursive method requiresBaselineTo avoid infinite loops (dogs chase their tails ).
Package algorithm. recursion; public class recursiondemo {/*** recursively calculates the nth element of the Fibonacci series, and N is based on the natural number of 1. */Public static int fibonacc (int n) {If (n <= 1) return N; else return fig (n-1) + fig (n-2 );} /*** iteratively calculates the nth element of the Fibonacci series, and N is based on the natural number of 1. */Public static int fibonacc1 (int n) {int first, second, result; first = Second = Result = 1; for (INT I = 3; I <= N; I ++) {result = first + second; first = Second; second = result;} return result ;}}In most cases, iteration and Recursion can convert each other.
There are two practical guidelines for using recursion:
1,Design first. Recursion can be used in any case, and simple and clear programming is preferred. Some problems, such as those that need to be regressed (such as finding the way out of the maze and performing operations on the tree) are difficult to solve if recursion is not used.
2,
Efficiency Balance. If repetitive work occurs in recursive calls, use a loop instead. The recursive method is not suitable for general numerical calculations.
The Java recursive method is throughMethod call stack. Set the breakpoint to run factorial (5) in bluej. The method call status is displayed. It just "rashly thinks" factorial (4) is known, and so on. To factorial (5), no multiplication is performed currently. There are six stack frames in the method call stack, and factorial (0) is calculated on the top layer ). The two phases of recursive methods are:Recurrence and Regression.
For example, if the recursive sum (n) = N + sum (n-1) is used, yqj2065 has seen an interesting question.
static long sum1(long a) { return (a == 1)? 1:(a + sum1(a - 1)); } static long sum2(long a) { return (a == 1)? 1:(sum2(a - 1) + a); }Are there any differences between the two?
Tower of Hanoi
Hanoi Tower Problem): There are three poles A, B, and C, and the strings on the pole a are several large dishes. Every time you move a plate, make sure that the tray can only be folded on the top of the tray, and use B for transition. Please move all the dishes from Rod a to Rod C.
For people with recursive thinking, even more dishes are just two parts: n-1-1 dishes above are regarded as small dishes sticking together, and below is a big dish. Recursive Algorithms for the tower of Hanoi Problem:
End condition: there is only one dish on pole a and it is moved to C.
Recursion:
1. Move N-1 1 plate above from the departure location a to the Transfer Station B;
2. Move the nth dish to the destination C;
3. Move n-1 dishes from Station B to the destination C.
Package algorithm. recursion; public class hanoitower {Private Static int step = 0;/** recursive demonstration of tower of death. * @ Param from the departure location of the dish * @ Param temp the transfer station * @ Param to the arrival location of the dish * @ Param n the number of dishes to be moved */static void Hanoi (char from, char temp, Char to, int N) {If (n = 1) {step ++; system. out. println ("Step + step +": "+ from +" → "+ to);} else {// move n-1 dishes to the transfer station, therefore, the current destination is temp. Hanoi (from, to, temp, n-1); // move the nth dish to the ground step ++; system. out. println ("Step + step +": "+ from +" → "+ to); // move n-1 dishes to the location. Hanoi (temp, from, to, n-1 );}}}Output of Hanoi ('A', 'B', 'C', 3:
Step 2: A → C
Step 2: A → B
Step 2: C → B
Step 2: A → C
Step 2: B →
Step 2: B → C
Step 2: A → C
The Iteration Algorithm of the tower of Hanoi Problem is complicated,
Recursion (recursion)