C++程序--汉罗塔问题实现程序代码



 

#include

#include

using namespace std;

int count=0;

 

void move(int n, char one, char three)

{

         cout<<"把第"<
         count=count+1;

}

 

void Hanoi(int n,char one,char two,char three)

//将n个盘子从one柱移至three柱(借助two柱)

{

         if(n==1)

         {

                   move(1,one,three);

         }

         else

         {

                   Hanoi(n-1,one,three,two);

                   move(n,one,three);

                   Hanoi(n-1,two,one,three);

         }

}

 

void main()

{

         int n;

         cout<<"请输入盘子的个数: ";

         cin>>n;

         Hanoi(n,'A','B','C');

         cout<<"\n\n\t。。。总共需要 "<
}

你可能感兴趣的:(C++程序--汉罗塔问题实现程序代码)