java實現的經典遞歸算法三例

地址:http://soft.chinabyte.com/database/291/11363791.shtml


一、寫作此文的原因:

  學過程序設計的朋友都知道,存在自調用的算法稱作遞歸算法。 遞歸往往能給我們帶來非常簡潔非常直觀的代碼形勢,從而使我們的編碼大大簡化,然而遞歸的思維確實很我們的常規思維相逆的,我們通常都是從上而下的思維問題, 而遞歸趨勢從下往上的進行思維,正由於此,很多人對於遞歸有着深深的恐懼,我曾經也是如此,如今爲把我的經驗通過幾個經典的例子與初學者共享,故作此文,希望能對需要者有所助益,如若如此,便是幸甚……

  二、遞歸算法設計的基本思想是:對於一個複雜的問題,把原問題分解爲若干個相對簡單類同的子問題,繼續下去直到子問題簡單到能夠直接求解,也就是說到了遞推的出口,這樣原問題就有遞推得解。

  關鍵要抓住的是:

  (1)遞歸出口

  (2)地推逐步向出口逼近

  三、具體說明

  1.漢諾塔

  這是遞歸的超經典的例子,幾乎每本程序設計書上談到遞歸都會介紹。具體情景不再贅述。以我上述的方法觀之:(1)遞歸的出口在於disk數爲一的時候

  (2)向出口逼近:如果不是一,是n ,則我們先挪動上面n-1塊disk,等上面挪完,即遞歸返回的時候,我們挪動最底下的disk.

  僅僅如此,一個貌似十分複雜的問題就解決了,因爲挪動那n-1塊disk的時候,會繼續向上減少,直到disk的數量爲一爲止。下面給出java程序編碼(已測試過,運行正常):

  import javax.swing.JOptionPane;

  public class Hanoi {

  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 disks you want me move.");

  int num=Integer.parseInt(input);

  move(num,from,mid,to);

  }

  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);

  }

  }

  }

  2.這是一個排列的例子,它所做的工作是將輸入的一個字符串中的所有元素進行排序並輸出,例如:你給出的參數是"abc" 則程序會輸出:

  abc

  acb

  bac

  bca

  cab

  cba

  (1)算法的出口在於:low=high也就是現在給出的排列元素只有一個時。

  (2)算法的逼近過程:先確定排列的第一位元素,也就是循環中i所代表的元素,

  然後low+1開始減少排列元素,如此下去,直到low=high

  public static void permute(String str) {

  char[] strArray = str.toCharArray();

  permute(strArray, 0, strArray.length - 1);

  }

  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;

  }

  }

  }

  3。這是一個組合的例子,與上述的例子相似,只是它所做的工作是,輸出所給字符串中制定數目的元素的組合種類

  (1)程序出口在於n=1,此時只要輸出目標數組的所有元素即可

  (2)逼近過程,當n>1 的時候,我們先取第一個元素放入目標數組中,然後n-1,如此下去,最後出來。

  import javax.swing.JOptionPane;

  public class Combination {

  /**

  * @param args

  */

  public static void main(String[] args) {

  String input = JOptionPane.showInputDialog("please input your String: ");

  String numString = JOptionPane.showInputDialog("please input the number of your Combination: ");

  int num = Integer.parseInt(numString);

  Combine(input, num);

  }

  private static void Combine(String input, int num) {

  char[] a = input.toCharArray();

  String b = "";

  Combine(a, num, b, 0, a.length);

  }

  private static void Combine(char[] a, int num, String b, int low, int high) {

  if (num == 0) {

  System.out.println(b);

  } else {

  for (int i = low; i < a.length; i++) {

  b += a[i];

  Combine(a, num - 1, b, i+1, a.length);

  b=b.substring(0, b.length()-1);

  }

  }

  }

  }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章