【正文】
直接把圓盤從 A移到 C。v n1時216。 把上面 n1個圓盤從 A移到 B216。 然后將 n號盤從 A移到 C216。 將 n1個盤從 B移到 C。v 即把求解 n個圓盤的 Hanoi問題轉(zhuǎn)化為求解 n1個圓盤的 Hanoi問題,依次類推,直至轉(zhuǎn)化成只有一個圓盤的 Hanoi問題。A B CDate 50Data Structure main() { int m。 printf(Input number of disks”)。 scanf(%d,amp。m)。 printf(”Steps : %3d disks”,m)。 hanoi(m,39。A39。,39。B39。,39。C39。)。(0) }void hanoi(int n,char x,char y,char z)(1) {(2) if(n==1)(3) move(1,x,z)。(4) else{(5) hanoi(n1,x,z,y)。(6) move(n,x,z)。(7) hanoi(n1,y,x,z)。(8) }(9) }A B C1233 A B C 03 A B C 02 A C B 63 A B C 02 A C B 61 A B C 6A B C3 A B C 02 A C B 6Date 51Data Structure main() { int m。 printf(Input the number of disks scanf(%d,amp。m)。 printf(The steps to moving %3d hanoi(m,39。A39。,39。B39。,39。C39。)。(0) }void hanoi(int n,char x,char y,char z)(1) {(2) if(n==1)(3) move(1,x,z)。(4) else{(5) hanoi(n1,x,z,y)。(6) move(n,x,z)。(7) hanoi(n1,y,x,z)。(8) }(9) }A B C3 A B C 02 A C B 61 C A B 8A B C3 A B C 02 A C B 63 A B C 03 A B C 02 A C B 6Date 52Data Structure main() { int m。 printf(Input the number of disks scanf(%d,amp。m)。 printf(The steps to moving %3d hanoi(m,39。A39。,39。B39。,39。C39。)。(0) }void hanoi(int n,char x,char y,char z)(1) {(2) if(n==1)(3) move(1,x,z)。(4) else{(5) hanoi(n1,x,z,y)。(6) move(n,x,z)。(7) hanoi(n1,y,x,z)。(8) }(9) }3 A B C 02 B A C 83 A B C 02 B A C 81 B C A 6A B C3 A B C 02 B A C 83 A B C 0A B CDate 53Data Structure main() { int m。 printf(Input the number of disks scanf(%d,amp。m)。 printf(The steps to moving %3d hanoi(m,39。A39。,39。B39。,39。C39。)。(0) }void hanoi(int n,char x,char y,char z)(1) {(2) if(n==1)(3) move(1,x,z)。(4) else{(5) hanoi(n1,x,z,y)。(6) move(n,x,z)。(7) hanoi(n1,y,x,z)。(8) }(9) }A B C3 A B C 02 B A C 81 A B C 8A B C3 A B C 02 B A C 83 A B C 0??? A B C 02 B A C 8Date 54Data Structure簡述以下算法的功能(棧和隊列的元素類型均為 int)void algo3(Queue amp。Q){Stack S。 int d。InitStack (S)。while (!QueueEmpty(Q)){ DeQueue(Q, d)。 Push(S, d)。}while (!StackEmpty(S)){ Pop(S, d)。 EnQueue(Q, d)。} }Date 55