小小hanoi
2016-01-15 00:00
441 查看
![](http://static.oschina.net/uploads/img/201601/15103724_QcQu.gif)
![](http://static.oschina.net/uploads/img/201601/15103724_j6Cx.gif)
View Code
#include " iostream " using namespace std; int k = 0 ; void hanoi( int m , char a , char b, char c) { if (m == 1 ) { k ++ ; printf( " %c->%c " ,a , c); return ; } hanoi(m - 1 , a, c , b); printf( " %c->%c " ,a , c); k ++ ; hanoi(m - 1 , b, a, c); } int main() { int n; char x,y,z; while (cin >> n) { x = ' A ' ;y = ' B ' ; z = ' C ' ; cout << " 移动过程:-------> " << endl; hanoi( n , x, y, z ); cout << endl; cout << " 移动次数:-----> " << endl; cout << k << endl; } return 0 ; }