Introduction of Recursive algorithm
In mathematics and computer science, recursion refers to the method of using the function itself in the definition of a function.
Recursive algorithm is a direct or indirect process to invoke its own algorithm. In the computer programming, the recursive algorithm is very effective to solve a large class of problems, it often makes the description of the algorithm concise and easy to understand.
The recursive algorithm solves the problem characteristic:
(1) Recursion is the invocation of itself in a procedure or function.
(2) When using a recursive strategy, there must be a definite recursive end condition called a recursive exit.
(3) Recursive algorithm is usually very concise, but the recursive algorithm is less efficient in solving problems. Therefore, the recursive algorithm is generally not advocated for the design of the program.
(4) in the process of recursive invocation, the system opens up a stack for each layer's return point, local quantity and so on to store. Too many recursion times can cause stack overflow and so on. Therefore, the recursive algorithm is generally not advocated for the design of the program. In the actual programming, especially pay attention to stack overflow problem.
With recursive method, we can transform a relatively complex problem into a small problem similar to the original problem to solve, the recursive method needs a small number of programs to describe the process of solving the problem of repeated calculations, greatly reducing the code of the program. However, there are some drawbacks when it comes to convenience, that is, the efficiency of using recursive method is not high.
1. Fibonacci-Cut Series (Fibonacci)
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ..., in short, is the sum of the number of nth (n > 2) equals the number of N-1 and (N-2).
(1) When no recursion is required
int[] A=New int[ -]; a[0] =1; a[1] =1; intQian = a[0]; intHou = a[1]; for(inti =2; i < a.length; i++) {A[i]= Qian +Hou; Qian=Hou; Hou=A[i]; } Console.WriteLine (a[6]);
View Code
(2) when using recursion
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacePractice Job {classProgram {Static voidMain (string[] args) { //Calling Methods intA = Foo ( -); Console.WriteLine (a); Console.ReadLine (); } //Method Public Static intFoo (inti) {if(I <=0) { return 0; } Else if(I >0&& I <=2) { return 1; } Else { returnFoo (I-2) + Foo (i-1); } } }}
View Code
2. Factorial
Factorial (!) is the product of all positive integers that are less than a certain number.
0! = 1
1! = 1
2! = 2 * 1! = 2
3! = 3 * 2! = 6
...
N! = n * (n-1)!
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacePractice Job {classProgram {Static voidMain (string[] args) { //Calling Methods intA = Jiecheng (3); Console.WriteLine (a); Console.ReadLine (); } //Method Public Static intJiecheng (intN) {intsum; if(n = =0)//restrictions that restrict the method call itself{sum=1; } Else{sum=n * Jiecheng (N-1); } returnsum; } }}
View Code
3, Hanoi Tower problem
There are three Poles a,b,c. A rod has N (n>1) perforated discs, the size of the disk from bottom to top in turn smaller. The following rules are required to move all discs to the C-bar:
1, can only move one disk at a time;
2, the market can not be stacked on the small plate above.
Tip: The disc can be temporarily placed in the B-bar, or the disc removed from the A will be moved back to the A-bar, but must follow the above two rules.
Q: How do I move? How many times do you want to move at least?
Static voidMain (string[] args) {Hanoi (5,'A','B','C'); Console.ReadLine (); } Public Static voidHanoi (intNCharACharBCharC) {//Hanoi Tower Problem//Move n plates from block A to block C with seat B. if(n = =1) Move (A, C); Else{Hanoi (n-1, A, C, B); Move (A, C); Hanoi (n-1, B, A, C); } } Public Static voidMove (CharStartplace,Charendplace) {Console.WriteLine ("Move {0} to {1}", Startplace,endplace); }
View Code
4. Find Files
PublicArrayList al=NewArrayList ();//I use ArrayList as a dynamic array, very useful Public voidGetalldirlist (stringStrbasedir) {DirectoryInfo di=NewDirectoryInfo (Strbasedir); Directoryinfo[] DiA=di. GetDirectories (); for(intI=0; i<dia.length;i++) {al. ADD (Dia[i]. FullName); //Dia[i]. FullName is the absolute address of a subdirectory and records it in ArrayListGetalldirlist (Dia[i]. FullName);//Note: Here's how C # recursion is used } }
View Code
Recursive algorithm Classic example Summary (C # Implementation)