请移步题目B3616【模板】队列
下面两份代码中,码一过不了,但是码二能过。原因如上所述。
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
queue<int> Q;
int n;
int opt, x;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n;
while(n --) {
cin >> opt;
if(opt == 1) {
cin >> x;
Q.push(x);
} else if(opt == 2) {
if(Q.empty()) {
puts("ERR_CANNOT_POP");
} else {
Q.pop();
}
} else if(opt == 3) {
if(Q.empty()) {
puts("ERR_CANNOT_QUERY");
} else {
cout << Q.front() << endl;
}
} else {
cout << Q.size() << endl;
}
}
return 0;
}
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
queue<int> Q;
int n;
int opt, x;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n;
while(n --) {
cin >> opt;
if(opt == 1) {
cin >> x;
Q.push(x);
} else if(opt == 2) {
if(Q.empty()) {
cout << "ERR_CANNOT_POP" << endl;
} else {
Q.pop();
}
} else if(opt == 3) {
if(Q.empty()) {
cout << "ERR_CANNOT_QUERY" << endl;
} else {
cout << Q.front() << endl;
}
} else {
cout << Q.size() << endl;
}
}
return 0;
}