用C++解决汉诺塔问题

/*
时间:2018.1.25
作者:小岛的水
*/

#include
using namespace std;
//调用递归函数解决汉诺塔问题
int i = 1;                  //全局变量来记录布数
int main()
{
 void move(int n, char from, char to);
 void hanoi(int n, char from, char zhongjian, char to);
 int n;
 cout << "请输入盘子个数:" << endl;
 cin >> n;
 char a = 'A', b = 'B', c = 'C';
 hanoi(n, a, b, c);
 return 0;

}
void move(int n,char from,char to)
{
 cout << "第" << i++ << "步:将第" << n << "号盘子" << from << "-->" << to << endl;
    //每调用一次move函数,步数加一
}
void hanoi(int n, char from, char zhongjian, char to)
{
 void move(int n, char from, char to);
 if (n == 1)
  move(1, from,to);
 else
 {
  hanoi(n - 1,from,zhongjian,to);
  move(n, from, to);
  hanoi(n - 1, zhongjian, from,to);
 }
}

你可能感兴趣的:(用C++解决汉诺塔问题)