题目链接 :点击打开题目
第一次用队列和栈, 做了这道水题。
根据题意,应该分为队列和栈两种情况,通过strcmp(,)可以比较两个字符串是否相等,并且需要注意的是相等时返回值为0,前者字典序大时返回1,小时返回-1.
AC代码:
#include<stdio.h>
#include<queue>
#include<stack>
#include<string.h>
using namespace std;
int main(){
int n,m,a;
char c[10]; //本来queue和stack的定义放到这里了,然后就喜闻乐见的runtime error了
scanf("%d",&n);
char str1[] = "FIFO",str2[] = "IN"; //利用strcmp进行比较, 也见到了用a[2]和'F'比较的骚操作。
while(n--){
scanf("%d %s",&m,c);
if(!strcmp(c,str1)){ //注意返回值为0, 取反
queue<int> q;
while(m--){
scanf("%s",c);
if(!strcmp(c,str2)){
scanf("%d",&a);
q.push(a);
}
else{
if(!q.empty()){
printf("%d\n",q.front());
q.pop();
}
else{
printf("None\n");
}
}
}
}
else{
stack<int> s;
while(m--){
scanf("%s",c);
if(!strcmp(c,str2)){
scanf("%d",&a);
s.push(a);
}
else{
if(!s.empty()){
printf("%d\n",s.top());
s.pop();
}
else{
printf("None\n");
}
}
}
}
}
return 0;
}