栈——火车出战入站问题
来源:互联网 发布:读普通话的软件 编辑:程序博客网 时间:2024/04/28 22:03
题目:
铁路进行列车调度时,常把站台设计成栈式结构的站台。试问:
(1)设有编号为1,2,3,4,5,6的六辆列车,顺序开入栈式结构的站台,则可能的出栈序列有多少种?
(2)若进站的六辆列车顺序如上所述,那么是否能够得到435612, 325641, 154623和135426的出站序列,如果不能,说明为什么不能;如果能,说明如何得到(即写出"进栈"或"出栈"的序列)。
代码:
#include <stdio.h>#include<iostream>using namespace std;typedef struct{ int *stk; int top; int size;} stack;void initstack(stack *s, int n){ s->stk = (int*)malloc((s->size=n) * sizeof(int)); s->top = 0;}void outputstack(stack* s){ int i;int count=1; cout<<count<<", "; for(i=0; i<s->top; i++) { cout<<s->stk[i]<<" "; } cout<<endl;}int stackempty(stack* s){ return !s->top;}void push(stack* s, int x){ s->stk[s->top++] = x;}int pop(stack* s){ return s->stk[--s->top];}void stackseq(stack *input, stack *s, stack *output){ if(stackempty(input) && stackempty(s)) outputstack(output); else { if(!stackempty(input)) { push(s, pop(input)); stackseq(input, s, output); push(input, pop(s)); } if(!stackempty(s)) { push(output, pop(s)); stackseq(input, s, output); push(s, pop(output)); } }}main() { int i; stack input, s, output; initstack(&input, 20); initstack(&s, 20); initstack(&output, 20); for(i=6; i>0; i--) push(&input, i); stackseq(&input, &s, &output); return 0; }
- 栈——火车出战入站问题
- 火车出站入站问题
- 火车入站
- CSU1757-火车入站-模拟
- CSU 1757 火车入站
- 火车进出站问题---栈
- 栈的应用——火车进站出站问题
- HDU-1022 火车进出站问题【栈】
- hdu 1022 火车 栈 问题
- 全排列——火车进站问题(华为OJ)
- HDU 1022 火车进出站问题
- HDU1022--火车进出站问题Ⅰ
- HDU 1022 火车进出站问题
- 火车进出站序列问题简述
- HDU-1022-火车进出站问题 .
- hdoj 1022 火车进出站问题
- HDU 1022 火车进出站问题
- 栈的思想解决火车出站问题
- explicit 显式转换 用法 没搞清楚了。
- Android Fragments 详细使用
- java中的IO整理
- 腾讯后台面试过程
- Java实现Excel中对一列排序,另一列数据相应改变的程序
- 栈——火车出战入站问题
- python unicode字节串转成中文问题
- 正则表达式规则(十一)
- [OGRE]基础教程来五发:来谈一谈蓝天白云
- 关于郭天祥单片机书上第十二页电平特性的相关参数
- 写2013年的国庆
- hadoop 文件分块,block与split关系
- js中undefined,null,NaN的区别
- 02 XNA Framework 常用的类别