First, the basic idea of recursive algorithm design is:
For a complex problem, the original problem is decomposed into a number of relatively simple and similar child problems, continue until the child problem is simple to be able to solve directly, that is, to the recursive export, so the original problem has recursive solution.
In the recursive algorithm, we must hold the exit, that is, to do the recursive algorithm must have a clear recursive termination conditions. This is very important. In fact, this exit is very good understanding, is a condition, when satisfied with this condition, we will not recursion.
The key to catch is:
(1) Recursive export
(2) Push forward to exit gradually
Second, recursive algorithm example
(1) Factorial:
Requirement: Given a number, calculate its factorial value, for example, 5 factorial is 5*4*3*2*1
Realize:
Using recursion to realize the factorial value of a number
private static BigDecimal Getnum (BigDecimal innum) {
if (Innum.compareto (Bigdecimal.one) = 0) {return
innum;
}
Return innum.multiply (Getnum (Innum.subtract (Bigdecimal.one)));
}
(2) Fibonacci series: 1,1,2,3,5,8,13 ...
Requirements: Find the value of the index specified in the series
Realize:
Using recursion to implement the Fibonacci series
private static int fab (int index) {
if (index = = 1 | | index = = 2) {return
1;
} else {return
fab (index-1) + fab (index-2);
}
(3) Hanoi
Requirements: Hanoi Mobile
Realize:
private static final String Disk_b = "DISKB";
private static final String Disk_c = "DISKC";
private static final String disk_a = "Diska";
Static String from=disk_a;
Static String To=disk_c;
Static String Mid=disk_b;
public static void Main (string[] args) {
String input=joptionpane.showinputdialog ("Please input the number of the disk s you want me move. ");
int num=integer.parseint (input);
Move (num,from,mid,to);
}
Using recursion to implement Hanoi
private static void move (int num, string from2, String Mid2, String to2) {
if (num = = 1) {
Sys TEM.OUT.PRINTLN ("Move Disk 1 from" + From2 + ' to "+ To2);
} else {move
(num-1, From2, To2, MID2);
SYSTEM.OUT.PRINTLN ("Move disk" + num + "from" + From2 + "to" + To2);
Move (Num-1, Mid2, From2, To2);
}
(4) Permutation combination
Requirements: Sort and output all the elements in a string you enter, for example: The argument you give is "ABC",
The program will output
Abc
Acb
Bac
Bca
Cab
Cba
Realize:
public static void Permute (String str) {
char[] Strarray = Str.tochararray ();
Permute (strarray, 0, strarray.length-1);
Using a recursive implementation, sort all the elements in the input string and output the public
static void Permute (char[] list, int low, int high) {
int i;
if (low = = high) {
String cout = "";
for (i = 0; I <= high; i++) {
cout + = List[i];
}
System.out.println (cout);
} else {for
(i = low; I <= high; i++) {
char temp = List[low];
List[low] = List[i];
List[i] = temp;
Permute (list, low + 1, high);
temp = List[low];
List[low] = List[i];
List[i] = temp;
}}}
Summing up the recursive algorithm, this root is the exit, as long as the exit is found, then the algorithm will naturally be able to.
Reference one: http://blog.csdn.net/lfsf802/article/details/7696405
Reference II, http://nevergives.blog.sohu.com/95934843.html