和书上写法不一样,输出的数是对的,但不确定能否通过测试。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
int main(){
int n, m, t, animal;
cin >> n;
queue<int> dogs;
queue<int> cats;
queue<int> all;
queue<int> ans;
while(n--){
cin >> m >> t;
if(m == 1){
if(t > 0){
dogs.push(t);
}else{
cats.push(t);
}
all.push(t);
}
if(m == 2 && !all.empty()){
if(t == 0 ){
animal = all.front();
if(animal > 0 && !dogs.empty()) {
dogs.pop();
ans.push(animal);
}
else if(animal < 0 && !cats.empty()){
cats.pop();
ans.push(animal);
}
all.pop();
}else if(t == 1 && !dogs.empty()){
ans.push(dogs.front());
dogs.pop();
all.pop();
}else if(t == -1 && !cats.empty()){
ans.push(cats.front());
cats.pop();
all.pop();
}
}
}
while(!ans.empty()){
printf("%d ", ans.front());
ans.pop();
}
return 0;
}
一些按照题目给的格式写的代码
https://blog.csdn.net/WYXHAHAHA123/article/details/97263876
题目下也有答案
https://www.nowcoder.com/questionTerminal/6235a76b1e404f748f7c820583125c50

京公网安备 11010502036488号