递归--汉诺塔问题

#include <iostream>

using namespace std;
int num=1;
void move(char getone,char putone)
{
	cout<<getone<<"-->"<<putone<<endl;
}
void hanoi(int n,char one,char two,char three)
{
	void move(char getone,char putone);
	
	if (n==1) move(one,three);
	else
	{
		hanoi(n-1,one,three,two);
		move(one,three);
		hanoi(n-1,two,one,three);
		num+=2;
	}
}

int main()
{
	void hanoi(int n,char one,char two,char three);
	int m;

	cout<<"Enter the number of diskes:";
	cin>>m;
	cout<<"the steps to moving "<<m<<" disk:"<<endl;

	hanoi(m,'A','B','C');

	cout<<"total steps is: "<<num<<endl;
	return 0;
}

    原文作者: 汉诺塔问题
    原文地址: https://blog.csdn.net/promenader/article/details/8793513
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞