火车进出站次序问题的一个程序,请指点!!

bary1980 bary1980 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#include<stack>
#include<iostream>

using namespace std;
stack<int> input; //来的火车
stack<int> station; //进站的火车
stack<int> output; //出站的火车
static int i;
#define car_number 3

void dot(stack<int> _input,stack<int> _station,stack<int> _output)
{
stack<int> a_input=_input;
stack<int> a_station=_station;
stack<int> a_output=_output;
stack<int> to_buffer;
//输入的时候
if (!_input.empty())
{
_station.push(_input.top());
_input.pop();
dot(_input,_station,_output);
};
//输出的时候
if (!a_station.empty())
{
a_output.push(a_station.top());
a_station.pop();
//************输出一个堆栈的所有元素***********
if (a_output.size()==car_number) {
while(!a_output.empty())
{
to_buffer.push(a_output.top());
a_output.pop();
}
while(!to_buffer.empty())
{
cout<<to_buffer.top();
to_buffer.pop();
}
cout<<endl;
}
//*********************************************
dot(a_input,a_station,a_output);
}

};

void main()
{
for(int i=car_number; i>0 ; i--)
{
input.push(i);
}

dot(input,station,output);
}
请各位指点一下,
谁还有其他的方法拿出来大家一起讨论讨论!
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
bary1980
数据结构与算法 33,027 成员 35,334 内容