poj 1363 Rails
2013-11-01 17:09
447 查看
栈实现火车进站,出站。注意输入输出格式。
#include <iostream> using namespace std; #define MAX 1001 #define STACK_ININ_SIZE 100 #define STACK_INCREAMT 10 typedef struct stack { int stacksize; int *base; int *top; }stack; void InitStack(stack &s) { s.base = (int *)malloc(STACK_ININ_SIZE*sizeof(int)); s.top = s.base; s.stacksize = STACK_ININ_SIZE; } void Pop(stack &s) { if (s.base == s.top) return; s.top--; } void Push(stack &s, int n) { if (s.top-s.base>=s.stacksize) { s.base = (int *)realloc(s.base, (s.stacksize+STACK_INCREAMT)*sizeof(int)); s.top = s.base + s.stacksize; s.stacksize += STACK_INCREAMT; } *s.top = n; s.top++; } int IsEmpty(stack &s) { if (s.base == s.top) return 1; else return 0; } stack s; int main() { int num, i, j, flag; int ch[MAX]; flag = 0; while (cin>>num && num) { if (flag==1) cout<<endl; flag = 1; while (1) { cin>>ch[1]; if (ch[1]==0) break; for (i=2; i<=num; i++) cin>>ch[i]; InitStack(s); j = 1; for (i=1; i<=num; i++) { Push(s, i); while (!IsEmpty(s)) { if (*(s.top-1)==ch[j]) { Pop(s); j++; } else break; } } if (IsEmpty(s)) cout<<"Yes"<<endl; else cout<<"No"<<endl; } } return 0; }
相关文章推荐
- POJ - 1363 Rails
- POJ 1363 Rails
- POJ-1363 Rails(栈应用)
- POJ 1363 Rails
- POJ 1363 Rails(栈)
- POJ 1363 Rails
- zoj 1259 || poj 1363 Rails(栈~!)
- poj 1363 Rails
- POJ1363 Rails 栈+模拟
- POJ 1363|Rails|栈
- poj 1363 Rails
- poj-1363-Rails(栈)
- Poj-1363-Rails-栈
- POJ 1363 Rails
- POJ1363 Rails【stack】【栈】
- poj 1363 Rails(栈)
- 栈,汽车进站出站:poj 1363 rails
- Rails(poj 1363 栈的应用)
- poj 1363 Rails in PopPush City &&【求堆栈中合法出栈顺序次数】
- POJ 1363 Rails