refresh的停车场(栈和队列的STL)
refresh的停车场
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描写叙述
因为土地有限,停车场内停车数量有限。可是要求进停车场的车辆过多。
当停车场满时,要进入的车辆会进入便道等待,最先进入便道的车辆会优先
输入
输出
演示样例输入
2 6 Add 18353364208 Add 18353365550 Add 18353365558 Add 18353365559 Del Out
演示样例输出
18353365558 18353364208
提示
来源
演示样例程序
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
using namespace std;
int main()
{
char str[20];
string s;
int flag=0;//用来标记栈和队列是否为空。如为空还继续进行Del或者Out操作,则输出Error;
int n,m,i;
while(~scanf("%d %d",&n,&m))
{
stack<string >p;//记得要在循环里面建栈和队列。wa3次,哭瞎了。
queue<string >q;
flag=0;
for(i=1; i<=m; i++)
{
cin>>str;
if(strcmp(str,"Add")==0)
{
cin>>s;
if(p.size()==n)//此时证明停车场满了
q.push(s);//则车辆进入便道
else
p.push(s);
}
else if(strcmp(str,"Del")==0)
{
if(p.empty())
flag=1;
else
{
p.pop();
if(!q.empty())
{
p.push(q.front());
q.pop();
}
}
}
else if(strcmp(str,"Out")==0)
{
if(q.empty())
flag=1;
else
q.pop();
}
}
if(flag)
cout<<"Error"<<endl;
else
{
while(!p.empty())
{
cout<<p.top()<<endl;
p.pop();
}
}
}
return 0;
}
浙公网安备 33010602011771号