java實現的經典遞迴演算法三例

來源:互聯網
上載者:User

一、寫作此文的原因:

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

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

  關鍵要抓住的是:

  (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   cout += list[i];

  System.out.println(cout);

  } else {

  for (i = low; 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   b += a[i];

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

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

  }

  }

  }

  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.