题解 | #模拟队列#
模拟队列
https://www.nowcoder.com/practice/1d0f4ff9b5db4a08afd8b2cddb8c4979
#include <bits/stdc++.h>
using namespace std;
class Queue {
private:
queue<long long> q;
public:
int push(long long x) {
q.push(x);
return q.size();
}
long long pop() {
if(q.empty()) return -1;
long long ret = q.front();
q.pop();
return ret;
}
long long clear() {
long long ret = 0;
while(!q.empty()) ret ^= q.front(),q.pop();
return ret;
}
};
int main() {
int q;
cin >> q;
Queue queue;
string s;
getline(cin, s);
while (q--) {
getline(cin, s);
if (s.find("PUSH") != string::npos) {
int idx = s.find(" ");
cout << queue.push(atoi(s.substr(idx + 1).c_str())) << endl;
} else if ("POP" == s) {
cout << queue.pop() << endl;
} else {
cout << queue.clear() << endl;
}
}
return 0;
}
查看7道真题和解析