问题:
汉诺塔。
#include <stdio.h> #include <stdlib.h> void hanoi(int N, char A, char B, char C); /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { int n; scanf("%d", &n); printf("the step to move %2d dishes are:\n", n); hanoi(n, 'A', 'B', 'C'); return 0; } void hanoi(int N, char A, char B, char C) { if(N==1) printf("move dish %d from %c to %c\n", N, A, C); else { hanoi(N-1, A, C, B);//A借助C将剩下的N-1个碟子转到B printf("move dish %d from %c to %c\n", N, A, C); hanoi(N-1, B, A, C);//B借助A将N-1个碟子转到C } } /* 5 the step to move 5 dishes are: move dish 1 from A to C move dish 2 from A to B move dish 1 from C to B move dish 3 from A to C move dish 1 from B to A move dish 2 from B to C move dish 1 from A to C move dish 4 from A to B move dish 1 from C to B move dish 2 from C to A move dish 1 from B to A move dish 3 from C to B move dish 1 from A to C move dish 2 from A to B move dish 1 from C to B move dish 5 from A to C move dish 1 from B to A move dish 2 from B to C move dish 1 from A to C move dish 3 from B to A move dish 1 from C to B move dish 2 from C to A move dish 1 from B to A move dish 4 from B to C move dish 1 from A to C move dish 2 from A to B move dish 1 from C to B move dish 3 from A to C move dish 1 from B to A move dish 2 from B to C move dish 1 from A to C -------------------------------- */
原文地址:http://blog.csdn.net/orangeisnotapple/article/details/44871677