Hanoi Tower problem is: There are ABC three pillars, a pillar from top to bottom stacked a lot of volume in turn, if the disk from a to C pillars, and still keep from top to bottom in order to increment.
Class hanio{public void Moveone (int n, string init, String desti) { //The case of only one plate System.out.println ("Move:" +n+ "from" + init+ "to" +desti);} public void Move (int n, string init, String temp, string desti) {if (n <= 0) {System.out.println ("number Error"), return; else if (n = = 1) {moveone (n,init,desti);} Else{move (n-1, init, Desti, temp);//First move the upper (n-1) tray from the init lever to the temp bar desti (n, Init, moveone) with the desti lever; Then move the plate numbered n from the init lever to the desti rod Move (n-1, temp, init, desti), and/or finally move the upper (N-1) plate from the temp lever to the desti lever with the init lever }}}class Hanioapp{public static void Main (String args[]) {Hanio Hanio = new Hanio (); Hanio.move (3, "A", "B", "C");}}
Operation Result:
Move:1 from A to C move:2 from a to B move:1 from C to B move:3 from A to C move:1 from B to A Move:2 from B to C Move:1 F Rom A to C
Java recursive implementation of the question of the Han-nuo tower