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 sub-problems, continue until the sub-problem is simple enough to be solved directly, that is, to the recursive exit, so that the original problem has a recursive solution.
In the recursive algorithm, it is necessary to grasp the exit, that is, to do recursive algorithm must have a definite recursive end condition. This is very important. In fact, this export is very well understood, is a condition, when we meet the conditions of the time we will no longer be recursive.
The key thing to catch is:
(1) Recursive export
(2) The ground pushes towards the exit gradually
Second, recursive algorithm example
(1) Factorial:
Requirement: Given a number, calculate its factorial value, for example, the factorial of 5 is 5*4*3*2*1
Realize:
[HTML]View Plaincopyprint?
- <span style="FONT-SIZE:12PX;" >//using recursion to implement a number's factorial value
- private static BigDecimal Getnum (BigDecimal innum) {
- if (Innum.compareto (bigdecimal.one) = = 0) {
- return innum;
- }
- Return innum.multiply (Getnum (Innum.subtract (Bigdecimal.one));
- }</span>
(2) Fibonacci series: 1,1,2,3,5,8,13 ...
Requirements: Find the value of the specified index position in the series
Realize:
[HTML]View Plaincopyprint?
- <span style="FONT-SIZE:12PX;" >//using recursion to implement the Fibonacci sequence
- private static int fab (int index) {
- if (index = = 1 | | index = = 2) {
- return 1;
- } else {
- Return Fab (index-1) + fab (index-2);
- }
- }</span>
(3) Hanoi
Requirements: Hanoi
Realize:
[HTML]View Plaincopyprint?
- <span style=> <span style= "White-space:pre" > </span>private static final string disk_b = "DiskB";
- <span style="White-space:pre"> </span>private static final String Disk_c = "DISKC";
- <span style="White-space:pre"> </span>private static final String disk_a = "Diska";
- <span style="White-space:pre"> </span>static String from=disk_ A
- <span style="White-space:pre"> </span> static String to= Disk_c;
- <span style="White-space:pre"> </span> static String mid=disk_b;
- <span style="White-space:pre"> </span> public static void Main (String [] args) {
- <span style= "White-space:pre" > </ span> string input=joptionpane.showinputdialog ("Please input the number of the disks you want me move. ");
- <span style="White-space:pre"> </span> int num=integer.parseint ( Input);
- <span style="White-space:pre"> </span> Move (num,from,mid,to);
- <span style="White-space:pre"> </span>}</span>
[HTML]View Plaincopyprint?
- <span style="FONT-SIZE:12PX;" >//Using recursion to implement Hanoi
- private static void move (int num, string from2, String Mid2, String to2) {
- if (num = = 1) {
- SYSTEM.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);
- }
- }</span>
(4) Permutations and combinations
Requirements: Sorts and outputs all elements of a string entered, for example: The parameter you give is "ABC",
The program will output
Abc
Acb
Bac
Bca
Cab
Cba
Realize:
[HTML]View Plaincopyprint?
- <span style=><span style=< Span class= "Attribute-value" > "White-space:pre" > </span>public static void permute (String STR) {
- <span style="White-space:pre"> </span> char[] strarray = Str.tochararray ();
- <span style="White-space:pre"> </span> Permute (strarray, 0, STRARRAY.LENGTH-1);
- <span style="White-space:pre"> </span;}</span>
[HTML]View Plaincopyprint?
- <span style="FONT-SIZE:12PX;" >//Use recursive implementations to sort and output all elements of a string entered
- public static void Permute (char[] list, int. Low, int.) {
- 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;
- }
- }
- }</span>
Summing up the recursive algorithm, this root is the exit, as long as the exit is found, then the algorithm will naturally be the natural.
Reference one: http://blog.csdn.net/lfsf802/article/details/7696405
Reference Two, http://nevergives.blog.sohu.com/95934843.html
Turn: Java Recursive algorithm example summary