当前位置: 代码迷 >> 综合 >> HDU-1702 ACboy needs your help again!
  详细解决方案

HDU-1702 ACboy needs your help again!

热度:98   发布时间:2024-01-29 22:16:44.0

question

模拟栈和队列,栈是FILO,队列是FIFO。

Sample Input

4
4 FIFO
IN 1
IN 2
OUT
OUT
4 FILO
IN 1
IN 2
OUT
OUT
5 FIFO
IN 1
IN 2
OUT
OUT
OUT
5 FILO
IN 1
IN 2
OUT
IN 3
OUT

Sample Output

1
2
2
1
1
2
None
2
3

code

#include<iostream>
#include<string>
#include<stack>
#include<queue>
//模拟栈和队列 
//思路:根据题目提示,分为队列和栈两种情况 
using namespace std;
int main(){int t,n,temp;cin>>t;//t表示一共有多少组 while(t--){stack<int>S;queue<int>Q;string str,str1; cin>>n>>str;//n表示每组有多少个操作for(int i=0;i<n;i++){if(str=="FIFO"){//队列的情况cin>>str1;if(str1=="IN"){cin>>temp;Q.push(temp);}if(str1=="OUT"){if(Q.empty()){cout<<"None"<<endl;}else{cout<<Q.front()<<endl;Q.pop();}} }else{//栈的情况cin>>str1;if(str1=="IN"){cin>>temp;S.push(temp);}if(str1=="OUT"){if(S.empty()){cout<<"None"<<endl;}else{cout<<S.top()<<endl;S.pop();}}} }}return 0;
}