JAVA遞迴演算法執行個體小結__演算法

來源:互聯網
上載者:User

一、遞迴演算法設計的基本思想是:

        對於一個複雜的問題,把原問題分解為若干個相對簡單類同的子問題,繼續下去直到子問題簡單到能夠直接求解,也就是說到了遞推的出口,這樣原問題就有遞推得解。

      在做遞迴演算法的時候,一定要把握住出口,也就是做遞迴演算法必須要有一個明確的遞迴結束條件。這一點是非常重要的。其實這個出口是非常好理解的,就是一個條件,當滿足了這個條件的時候我們就不再遞迴了。
關鍵要抓住的是:
(1)遞迴出口

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


二、遞迴演算法執行個體

(1)階乘:

          要求:給定一個數值,計算出它的階乘值,例如5的階乘為5*4*3*2*1

          實現:

// 利用遞迴實現一個數的階乘值private static BigDecimal getNum(BigDecimal inNum) {if (inNum.compareTo(BigDecimal.ONE) == 0) {return inNum;}return inNum.multiply(getNum(inNum.subtract(BigDecimal.ONE)));}

(2)Fibonacci數列:1,1,2,3,5,8,13……

           要求:找出數列中指定index位置的數值

           實現:

// 利用遞迴實現了Fibonacci數列private static int fab(int index) {if (index == 1 || index == 2) {return 1;} else {return fab(index - 1) + fab(index - 2);}}

(3)漢諾塔

          要求:漢諾塔挪動

          實現:

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

(4)排列組合

          要求:將輸入的一個字串中的所有元素進行排序並輸出,例如:你給出的參數是"abc",

                      則程式會輸出

                      abc
                      acb
                      bac
                      bca
                      cab
                      cba

          實現:

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


總結遞迴演算法來說,這個根就是那個出口,只要找到這個出口所在,那麼演算法自然而然就能水到渠成了。

參考資料一:http://blog.csdn.net/lfsf802/article/details/7696405

參考資料二、http://nevergives.blog.sohu.com/95934843.html


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.