读书人

汉诺塔 与 递归编程(转从百度百科)

发布时间: 2012-08-27 21:21:57 作者: rapoo

汉诺塔 与 递归编程(转自百度百科)

  public class Hanoi {/**  *  * @param n  * 盘子的数目  * @param origin  * 源座  * @param assist  * 辅助座  * @param destination  * 目的座  */  public void hanoi(int n,char origin,char assist,char destination) {  if (n == 1) {  move(origin,destination);  } else {  hanoi(n - 1,origin,destination,assist);  move(origin,destination);  hanoi(n - 1,assist,origin,destination);  }  }  // Print the route of the movement  private void move(char origin,char destination) {  System.out.println("Direction:" + origin + "--->" + destination);  }  public static void main(String[] args) {  Hanoi hanoi = new Hanoi();  hanoi.hanoi(3,'A','B','C');  }  }

读书人网 >编程

热点推荐