火车进出站次序问题的一个程序,请指点!!
#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);
}
请各位指点一下,
谁还有其他的方法拿出来大家一起讨论讨论!
#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);
}
请各位指点一下,
谁还有其他的方法拿出来大家一起讨论讨论!
...全文
相关推荐
火车进站出站次序问题
火车进站出站次序问题的实现代码,用三个栈来实现的
栈混洗——火车进站出站的可能次序
栈混洗的实现。 火车依次进入一个车站,以“后进先出”的次序出站,求解火车可能的次序。
基于51单片机火车进出站proteus仿真设计(包含仿真及源程序)
基于51单片机火车进出站proteus仿真设计(包含仿真及源程序)
火车进出站顺序验证[可运行源码]
该文章介绍了一个关于火车进出站顺序的算法问题。题目要求验证给定的火车进站顺序是否可以通过栈的操作调整为指定的出站顺序。输入包括火车的数量和进站、出站序列,输出为是否可以调整以及具体的进出站操作顺序。文章提供了详细的代码实现,使用栈数据结构来模拟火车的进出站过程,并通过比较栈顶元素与出站序列的元素来判断是否可以完成调整。代码示例展示了如何通过进栈和出栈操作来验证出站顺序的可行性,并输出相应的结果。
plc控制火车进出站系统火车进出站信号控制与优化.doc
plc控制火车进出站系统火车进出站信号控制与优化.doc


