1 #include <iostream> 2 #include <cstdlib> 3 #include <cstdio> 4 5 using namespace std; 6 7 int step = 1; 8 void move(char from, char to) 9 { 10 printf("%d: %c->%c\n", step++, from, to); 11 } 12 void h(int n, char from, char via, char to) 13 { 14 if (n == 1) {move(from, to); return;} 15 h(n-1, from, to, via); 16 move(from, to); 17 h(n-1, via, from, to); 18 } 19 int main(void) 20 { 21 int n; 22 while (cin >> n) 23 { 24 step = 1; 25 h(n, 'x', 'y', 'z'); 26 } 27 28 return 0; 29 }
挺简单的一个Hanoi程序