You can remember the Spring Festival evening show. How many steps does it take to put an elephant in the refrigerator?
Today, I'm going to write about Hanoi, with three magical pillars: honesty, bravery, integrity. One of the pillars from the big to the small, from the ground up in turn emitted four magic rings, you need to the four magic rings on the large top of the small, can not be magnified on the small;
import Java.util.Scanner; Public classSSR {Static intStep =0; Public Static voidMain (string[] args) {//HanoiHanio (4,"Honest","Brave","Integrity"); } Public Static voidHanio (intnum, string A, string B, string c) { if(num = =1) {move (NUM,A,C); } Else{Hanio (num-1, A, C, b); Move (NUM,A,C); Hanio (Num-1, B, A, c); } } Public Static voidMoveintnum, String a,string b) {Step++ ; System. out. println ("Section"+step+"Step, plate"+num+"from"-am"the tower moves to"+b+"Tower"); } }
Operation Result:
1th, plate 1 moves from the Honest Tower to the 2nd step of the Brave tower, plate 2 from the Honest Tower to the 3rd step of the Upright tower, plate 1 from the brave tower to the upright tower 4th step, plate 3 from the honest Tower to the Brave Tower 5th step, plate 1 from the upright tower to the Honest Tower 6th step, plate 2 from the upright tower to the Plate 1 moved from the Honest Tower to the 8th step of the Brave tower, plate 4 moved from the Honest Tower to the 9th step of the Upright tower, plate 1 moved from the brave tower to the upright tower 10th step, plate 2 from the brave tower to the faithful tower 11th step, plate 1 from the upright tower to the Honest Tower 12th step, plate 3 from the brave tower to the Plate 1 move from the Honest Tower to the 14th step of the Brave tower, plate 2 moves from the Honest tower to the 15th step of the upright tower, plate 1 moves from the brave tower to the upright tower
Isn't it interesting?
java--Hanoi