Legend has it that in the ancient Indian Sheng Miao, a yellow copper plate was planted, and three pillars were inserted on the board (A, B and C pillars), and on a treasure pillar, from top to bottom, there were 64 gold plates in the order from small to large. The 64 gold plates on the A pillar are required to be moved to the C column according to the following rules.
③ at any time the market can not be placed on the small plate.
Task: Enter a positive integer n (the number of plates on a column), and the output moves to the C post.
Analysis: Set A on a there are n plates.
1. When N=1, move the disc from a directly to C.
2. When n=2, then:
① move the 1 discs on a to B;
The ② then moves a disc on a to C;
The ③ finally moves the 1 discs on the B to C.
★ ¡ï have the code is good to do.
Code to serve.
#include <stdio.h>voidHanoiintNCharACharBCharC) { if(n==1) printf ("%c→%c", A,c); Else{Hanoi (n-1, a,c,b); printf ("%c→%c\n", A,c); Hanoi (n-1, B,a,c); }}intMain () {Hanoi ( -,'X','Y','Z'); }
Downloads for C + + and dev-c++
Hanoi Tower Problem