refresh的停车场
題目描述
refresh最近發了一筆橫財,開了一家停車場。由于土地有限,停車場內停車數量有限,但是要求進停車場的車輛過多。當停車場滿時,要進入的車輛會進入便道等待,最先進入便道的車輛會優先 進入停車場,而且停車場的結構要求只出去的車輛必須是停車場中最后進去的車輛。現告訴你停車場容量N以及命令數M,以及一些命令(Add num 表示車牌號為num的車輛要進入停車場或便道, Del 表示停車場中出去了一輛車,Out 表示便道最前面的車輛不再等待,放棄進入停車場)。假設便道內的車輛不超過1000000.輸入
輸入為多組數據,每組數據首先輸入N和M(0< n,m <200000),接下來輸入M條命令。輸出
輸入結束后,如果出現停車場內無車輛而出現Del或者便道內無車輛而出現Out,則輸出Error,否則輸出停車場內的車輛,最后進入的最先輸出,無車輛不輸出。示例輸入
2 6 Add 18353364208 Add 18353365550 Add 18353365558 Add 18353365559 Del Out示例輸出
1835336555818353364208
#include <stdio.h> #include <stdlib.h> #include <string.h> #define stackmax 10000 #define stacknum 11111 typedef long long int ElenType; typedef struct {ElenType *base;ElenType *top;int stacksize; }SqStack; int InitStack(SqStack &S)//棧的初始化; {S.base=(ElenType *)malloc(sizeof(ElenType)*stacknum);if(!S.base) exit(0);S.top=S.base;S.stacksize=stacknum;return 1; } void push(SqStack &S,ElenType &e)//進棧; {if(S.top-S.base>=S.stacksize){S.base=(ElenType *)realloc(S.base,sizeof(ElenType)*(stacknum+stackmax));if(!S.base) exit(0);S.top=S.base+S.stacksize;S.stacksize+=stackmax;}*S.top++=e; } int pop(SqStack &S)//出棧; {if(S.top==S.base)return 0;S.top--;return 1; } int StackEmpty(SqStack &S)//判斷棧是否為空棧; {if(S.top==S.base)return 1;elsereturn 0; } void print(SqStack &S)//棧的元素的輸出; {while(!StackEmpty(S)){S.top--;printf("%lld\n",*S.top);} } typedef long long int QElemType; typedef long long ?int Status; typedef struct QNode {QElemType data;QNode *next; } QNode, *Queueptr; typedef struct {Queueptr front;Queueptr rear; } LinkQueue; Status InitQueue (LinkQueue &q)//隊的初始化; {q.front=q.rear=(Queueptr)malloc(sizeof(QNode));if(!q.front) exit(0);q.front->next=NULL;return 1; } Status EnQueuer(LinkQueue &q, QElemType &e)//進隊; {Queueptr p;p=(Queueptr)malloc(sizeof(QNode));if(!p) exit(0);p->data=e;p->next = NULL;q.rear->next=p;q.rear=p;return 1; } Status DeQueuel(LinkQueue &Q,QElemType &e)//出隊; {Queueptr p;if(Q.front==Q.rear)return 0;p=Q.front->next;e=p->data;Q.front->next=p->next;if(Q.rear==p)Q.rear=Q.front;free(p);return 1; } int EmptyQueue(LinkQueue q)//判斷是否為空隊; {if(q.front==q.rear)return 1;elsereturn 0; } QElemType Queuelength(LinkQueue q)//隊的長度‘ {QElemType i=0;Queueptr p;p=q.front;while(p!=q.rear){i++;p=p->next;}return i; } int main() {long int i,n,m;long long int num;char c[4];while(~scanf("%ld%ld",&n,&m)) {SqStack S;InitStack(S);LinkQueue Q;InitQueue(Q);int flag=1;for(i=1; i<=m; i++){scanf("%s", c);if(strcmp(c,"Add")==0){scanf("%lld",&num);if(S.top-S.base<n)//判斷棧停車位是否已滿,push(S, num);elseEnQueuer(Q,num);}if(strcmp(c,"Del")==0){if(StackEmpty(S))flag=0;//標記不合法的命令;else{pop(S);DeQueuel(Q,Q.front->data);push(S,Q.front->data);}}if(strcmp(c,"Out")==0){if(EmptyQueue(Q))flag=0;//標記不合法的命令;else{DeQueuel(Q,Q.front->data);}}}if(flag==0)printf("Error\n");elseprint(S);//棧內元素的輸出; } } #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <queue> #include <stack> using namespace std; int main() {int N,M,x,t;while(~scanf("%d %d",&N,&M)){char minglin[10],num[100];queue<string>qd;//隊列的定義;stack<string>sz;//棧的定義;t=0;while(M--){scanf("%s",minglin);if(minglin[0]=='A'){scanf("%s",num);x=sz.size();//棧的長度;if(x<N)sz.push(num);//進棧;elseqd.push(num);//進隊;}else if(minglin[0]=='D'){if(sz.empty())//空棧;t=1;else{sz.pop();//出棧;if(!qd.empty()){sz.push(qd.front());//進棧;qd.pop();//出隊;}}}else if(minglin[0]=='O'){if(qd.empty())t=1;elseqd.pop();}}if(t==1)printf("Error\n");else{while(!sz.empty()){cout<<sz.top()<<endl;sz.pop();}}}return 0; }
總結
以上是生活随笔為你收集整理的refresh的停车场的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++之带有默认参数值的构造函数
- 下一篇: MyBatis之快速入门