您的位置:首页 > 大数据 > 人工智能

hdu 1702 ACboy needs your help again

2014-02-22 01:37 330 查看
点击打开链接

 

题目大意:题目还是比较好理解的,通过给出的是FIFO 还是FILO,  然后再给出输入的数据给出输出结果;通过STL  栈和队列的使用很容易能够把题目解决了。

代码如下:

#include <iostream>
#include <string>
#include <queue>
#include <stack>
using namespace std;
void que_solve(int n)
{
queue<int>Q;
char s[10];
int a;
while(n--)
{
cin>>s;
if(!strcmp(s, "IN"))
{
cin>>a;
Q.push(a);
}
else if(!strcmp(s, "OUT"))
{
if(Q.empty())
cout<<"None"<<endl;
else
{
cout<<Q.front()<<endl;
Q.pop();
}
}
}
}
void sta_solve(int n)
{
stack<int>S;
char s[10];
int a;
while(n--)
{
cin>>s;
if(!strcmp(s, "IN"))
{
cin>>a ;
S.push(a);
}
else if(!strcmp(s, "OUT"))
{
if(S.empty())
cout<<"None"<<endl;
else
{
cout<<S.top()<<endl;
S.pop();
}
}
}
}
int main()
{
int n;
while(cin>>n)
{
while(n--)
{
int m;
char str[100];
cin>>m>>str;
if(!strcmp(str,"FIFO"))
que_solve(m);
else if(!strcmp(str,"FILO"))
sta_solve(m);
else
cout<<"error"<<endl;
}
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: