车厢重排.cpp

来自「车厢重排,一个c++数据结构习题」· C++ 代码 · 共 43 行

CPP
43 行
字号
#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 + =
减小字号Ctrl + -
显示快捷键?