来自「经典的C++编写的堆栈式汉诺塔的求解」· 代码 · 共 74 行

TXT
74
字号
#include "iostream.h"
#include "math.h"
#include "链式堆栈类.h"

struct hnot
{
	char to;
	char help;
	char now;
	int n;
};
void main()
{
	hnot now,next;
	char nowh[10];
	int i,num;
	now.now='A';
	now.help='B';
	now.to='C';
	now.n=10;
	num=now.n;
	for(i=0;i<=10;i++)
	{
		nowh[i]='A';
		
	}
	stacklist<hnot> hn;
	i=0;
	hn.push(now);
	while(hn.topnum)
	{
		now=hn.peek();
		if (now.n==1)
		{

			cout<<now.n<<now.now<<"->"<<now.to<<endl;
			nowh[now.n]=now.to;
			continue;
			
		}
		if (now.n>0)
		{
			if (nowh[now.n-1]==now.help) 
			{				
				cout<<now.n<<now.now<<"->"<<now.to<<endl;
				nowh[now.n]=now.to;
				next.n=now.n-1;
				next.to=now.to;
				next.help=now.now;
				next.now=now.help;	
				hn.push(next);
				continue;
			}
			else
			{

				hn.push(now);
				next.n=now.n-1;
				next.to=now.help;
				next.help=now.to;
				next.now=now.now;
				hn.push(next);
				continue;
			}
			
		}
		

	}




}

⌨️ 快捷键说明

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