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 ;
}
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 ;
}