小小hanoi

ContractedBlock.gif ExpandedBlockStart.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 ;
}

转载于:https://www.cnblogs.com/FCWORLD/archive/2011/05/10/2042448.html

你可能感兴趣的:(小小hanoi)