⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hanio.cpp

📁 解决汉诺塔问题的cpp文件
💻 CPP
字号:
/*#include<iostream.h>
#define maxlen 50
void hano(int n,char a,char b,char c)
{
	if(n==1)
	cout<<"将第"<<n<<"个圆盘从"<<a<<"移到"<<c<<endl;
	else
	{  hano(n-1,a,c,b);
	   cout<<"将第"<<n<<"个圆盘从"<<a<<"移到"<<c<<endl;
	   hano(n-1,b,a,c);
	}
}
void hanota(int n,char a,char b,char c)
{
	struct stack
	{int no,ns;
	 char x,y,z;
	};
	stack st[maxlen];
	int top=1;
	int n1,a1,b1,c1;
	st[top].no=1;
	st[top].ns=n;
	st[top].x=a;
    st[top].y=b;
    st[top].z=c;//
	while(top>0)
	{   if(st[top].no==1)
	{
		n1=st[top].ns;
		a1=st[top].x;
        b1=st[top].y;
        c1=st[top].z;
		//top--;
		//top++;
		st[top].no=1;
	    st[top].ns=n1-1;
    	st[top].x=b1;
        st[top].y=a1;
        st[top].z=c1;
		top++;
		st[top].no=0;
	    st[top].ns=n1;
    	st[top].x=a1;
        st[top].y=c1;
		top++;
        st[top].no=1;
	    st[top].ns=n1-1;
    	st[top].x=a1;
        st[top].y=c1;
		st[top].z=b1;
	}
	while(top>0&&(st[top].no==0||st[top].ns==1))
	{
		if(top>0&&st[top].no==0)
		{
			cout<<"将第"<<st[top].ns<<"个圆盘从"<<st[top].x<<"移到"<<st[top].y<<endl;
			top--;
		}
		if(top>0&&st[top].ns==1)
		{
			cout<<"将第"<<st[top].ns<<"个圆盘从"<<st[top].x<<"移到"<<st[top].z<<endl;
			top--;
		}
	}
	}
}
 


void main()
{
	int n;
	cout<<"please enter the order of hanoi"<<endl;
	cin>>n;
	hano(n,'A','B','C');
	cout<<"the same answer"<<endl;
	hanota(n,'A','B','C');
}
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -