• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • 什么是遞歸算法:對遞歸的理解(2)

    發表于:2013-01-09來源:Csdn作者:wangjinyu501點擊數: 標簽:算法遞歸
    System.out.println(result); } public int factorial(int index){ if(index==1){ return 1; }else{ return factorial(index-1)*index; } } } 程序執行流程如下: ③列出某個目錄下所有的子目錄

      System.out.println(result);

      }

      public int factorial(int index){

      if(index==1){

      return 1;

      }else{

      return factorial(index-1)*index;

      }

      }

      }

      程序執行流程如下:

     ?、哿谐瞿硞€目錄下所有的子目錄和文件

      求解代碼:

      [html] view plaincopyprint?

      /*

      * time:2012.12.2

      * author:王金宇

      * description:列出某個目錄下所有的子目錄和文件

      */

      public class ListDir {

      static void getDir(String strPath) throws Exception {

      try {

      File f = new File(strPath);

      if (f.isDirectory()) {

      File[] fList = f.listFiles();

      for (int j = 0; j < fList.length; j++) {

      if (fList[j].isDirectory()) {

      System.out.println(fList[j].getPath());

      getDir(fList[j].getPath()); // 在getDir函數里面又調用了getDir函數本身

      }

      }

      for (int j = 0; j < fList.length; j++) {

      if (fList[j].isFile()) {

      System.out.println(fList[j].getPath());

      }

      }

      }

      } catch (Exception e) {

      System.out.println("Error: " + e);

      }

      }

      public static void main(String[] args) {

      String strPath = "E:";

      System.out.println(strPath);

      try {

      getDir(strPath);

      } catch (Exception e) {

      }

      }

      }

      /*

      * time:2012.12.2

      * author:王金宇

      * description:列出某個目錄下所有的子目錄和文件

      */

      public class ListDir {

      static void getDir(String strPath) throws Exception {

      try {

      File f = new File(strPath);

      if (f.isDirectory()) {

      File[] fList = f.listFiles();

      for (int j = 0; j < fList.length; j++) {

      if (fList[j].isDirectory()) {

      System.out.println(fList[j].getPath());

      getDir(fList[j].getPath()); // 在getDir函數里面又調用了getDir函數本身

      }

      }

      for (int j = 0; j < fList.length; j++) {

      if (fList[j].isFile()) {

      System.out.println(fList[j].getPath());

      }

      }

      }

      } catch (Exception e) {

      System.out.println("Error: " + e);

      }

      }

      public static void main(String[] args) {

      String strPath = "E:";

      System.out.println(strPath);

      try {

      getDir(strPath);

      } catch (Exception e) {

      }

      }

      }

      這個流程圖你懂得,看文件數目了,大家自己分析吧。

     ?、軡h諾塔問題

      這是遞歸的超經典的例子,幾乎每本程序設計書上談到遞歸都會介紹。具體情景不再贅述。以我上述的方法觀之:

      (1)遞歸的出口在于盤子數為1的時候 。

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

      求解代碼:

      [html] view plaincopyprint?

      import javax.swing.JOptionPane;

      /*

      * time:2012.12.2

      * author:王金宇

      * description:

      */

      public class Hanoi {

      private final static String from = "盤子B";

      private final static String to = "盤子C";

      private final static String mid = "盤子A";

      public static void main(String[] args) {

      String input = JOptionPane.showInputDialog("請輸入你要移動的盤子數");

      int num = Integer.parseInt(input);

      Hanoi.move(num, from, mid, to);

      }

      private static void move(int num, String from2, String mid2, String to2) {

      if (num == 1) {

      System.out.println("移動盤子1 從" + from2 + "到" + to2);

      } else {

      move(num - 1, from2, to2, mid2);

      System.out.println("移動盤子" + num + " 從" + from2 + "到" + to2);

      move(num - 1, mid2, from2, to2);

      }

      }

      }

      import javax.swing.JOptionPane;

      /*

      * time:2012.12.2

      * author:王金宇

      * description:

      */

      public class Hanoi {

      private final static String from = "盤子B";

    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>