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

📄 车厢重排.cpp

📁 车厢重排,一个c++数据结构习题
💻 CPP
字号:
#include<iostream.h>
void main()
{int i,top[4],H[4][10],p[11],t,c,x,BestTop,BestTrack,j;
 int NowOut=1;
 int minH=10;
 int minS;
 cout<<"Please input the order before the rearrangement(n<=9):";
 for(i=9;i>=1;i--)
	 cin>>p[i];
 for(i=1;i<=3;i++)
	 top[i]=0;
 for(j=1;j<=9;j++)
	{if(p[j]==NowOut)
		{cout<<"Move car "<<p[j]<<" from input to output"<<endl;
		NowOut++;
		while(minH==NowOut)
			{top[minS]--;
			 cout<<"Move car "<<minH<<" from holding track "<<minS<<" to output"<<endl;
			 minH=11;
			 for(i=1;i<=3;i++)
				if((top[i]!=0)&&(t=H[i][top[i]])<minH)
					{minH=t;minS=i;}
				NowOut++;}}
	
	else {BestTrack=0;c=p[j];
			BestTop=10;
			for(i=1;i<=3;i++)
			{if(top[i]!=0)
				{x=H[i][top[i]];
				 if((c<x)&&(x<BestTop))
					{BestTop=x;BestTrack=i;}} 
		else
			if(top[i]==0)	
				if(!BestTrack)
					BestTrack=i;}
		   if(!BestTrack)
			{cout<<"This order can not be rearranged by three holding tracks"<<endl;break;}
			top[BestTrack]++;H[BestTrack][top[BestTrack]]=c;
			cout<<"Move car "<<c<<" from input to the holding track "<<BestTrack<<endl;
			if(c<minH)
			{minH=c;minS=BestTrack;}}} 
	
}

⌨️ 快捷键说明

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