Java recursion recursion of Hanoi Tower

From , 3 Years ago, written in Java, viewed 97 times.
URL https://pastebin.vip/view/d93ed5b6
  1. package algorithm.recursion;
  2. public class HanoiTower{
  3.     private static int step= 0;  
  4.     /**汉诺塔的递归演示。
  5.      * @param from  碟子的出发地
  6.      * @param temp  碟子的中转站
  7.      * @param to   碟子的到达地
  8.      * @param n  要移动的碟子个数
  9.      */
  10.     static void  hanoi(char from, char temp, char to, int n){
  11.         if (n == 1) {
  12.            step++;
  13.            System.out.println("第"+step+ "步: "+ from+"→"+ to);
  14.         }else {
  15.             //将n-1个碟子移到中转站,故目前的到达地是temp。
  16.             hanoi(from, to,temp,n-1);
  17.             //第n个碟子移到到达地
  18.             step++;
  19.             System.out.println("第"+step+ "步: "+ from+"→"+ to);
  20.             //将n-1个碟子移到到达地。
  21.             hanoi(temp,from,to,n-1);
  22.         }
  23.     }
  24. }

Reply to "Java recursion recursion of Hanoi Tower"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website