Turn: Java Recursive algorithm example summary

Source: Internet
Author: User

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?
  1. <span style="FONT-SIZE:12PX;" >//using recursion to implement a number's factorial value
  2. private static BigDecimal Getnum (BigDecimal innum) {
  3. if (Innum.compareto (bigdecimal.one) = = 0) {
  4. return innum;
  5. }
  6. Return innum.multiply (Getnum (Innum.subtract (Bigdecimal.one));
  7. }</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?
  1. <span style="FONT-SIZE:12PX;" >//using recursion to implement the Fibonacci sequence
  2. private static int fab (int index) {
  3. if (index = = 1 | | index = = 2) {  
  4. return 1;
  5. } else {
  6. Return Fab (index-1) + fab (index-2);
  7. }
  8. }</span>

(3) Hanoi

Requirements: Hanoi

Realize:

[HTML]View Plaincopyprint?
  1. <span style=>  <span  style= "White-space:pre" >   </span>private static final  string disk_b =  "DiskB";   
  2. <span style="White-space:pre"> </span>private static final String   Disk_c = "DISKC";
  3. <span style="White-space:pre"> </span>private static final  String disk_a = "Diska";
  4. <span style="White-space:pre"> </span>static String from=disk_  A
  5. <span style="White-space:pre"> </span> static String to=   Disk_c;
  6. <span style="White-space:pre"> </span> static String mid=disk_b;
  7. <span style="White-space:pre"> </span> public static void Main (String [] args) {
  8. <span style= "White-space:pre" >  </ span>      string  input=joptionpane.showinputdialog ("Please input  the number of the disks you want me move. ");   
  9. <span style="White-space:pre"> </span> int num=integer.parseint (  Input);
  10. <span style="White-space:pre"> </span> Move (num,from,mid,to);
  11. <span style="White-space:pre"> </span>}</span>

[HTML]View Plaincopyprint?
  1. <span style="FONT-SIZE:12PX;" >//Using recursion to implement Hanoi
  2. private static void move (int num, string from2, String Mid2, String to2) {
  3. if (num = = 1) {
  4. SYSTEM.OUT.PRINTLN ("Move Disk 1 from" + From2 + "to" + To2);
  5. } else {
  6. Move (Num-1, From2, To2, MID2);
  7. SYSTEM.OUT.PRINTLN ("Move disk" + num + "from" + From2 + "to" + To2);
  8. Move (Num-1, Mid2, From2, To2);
  9. }
  10. }</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?
  1. <span style=><span style=< Span class= "Attribute-value" > "White-space:pre" >     </span>public static void permute (String  STR)  {  
  2. <span style="White-space:pre"> </span> char[] strarray =   Str.tochararray ();
  3. <span style="White-space:pre"> </span> Permute (strarray, 0,  STRARRAY.LENGTH-1);
  4. <span style="White-space:pre"> </span;}</span>
[HTML]View Plaincopyprint?
  1. <span style="FONT-SIZE:12PX;" >//Use recursive implementations to sort and output all elements of a string entered
  2. public static void Permute (char[] list, int. Low, int.) {
  3. int i;
  4. if (low= = high) {
  5. String cout = "";
  6. for (i = 0; I <= high; i++) {
  7. cout + = List[i];
  8. }
  9. System.out.println (cout);
  10. } else {
  11. for (i = Low , I <= high; i++) {
  12. Char temp = List[low];
  13. List[low] = List[i];
  14. List[i] = temp;
  15. Permute (list, low + 1, high);
  16. temp = List[low];
  17. List[low] = List[i];
  18. List[i] = temp;
  19. }
  20. }
  21. }</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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.