123456789000
2026-02-04 15:28:33
发布于:广东
#include <iostream>
#include <queue>
#include <string>
#include <sstream>
using namespace std;
int main() {
queue<string> q;
string inputLine;
while (getline(cin, inputLine)) {
if (inputLine == "end") {
if (!q.empty()) {
while (!q.empty()) {
cout << q.front();
q.pop();
if (!q.empty()) {
cout << " ";
}
}
cout << endl;
}
break;
} else if (inputLine.substr(0, 3) == "out ") {
int k = stoi(inputLine.substr(3));
queue<string> tempOutput;
for (int i = 0; i < k && !q.empty(); ++i) {
tempOutput.push(q.front());
q.pop();
}
if (tempOutput.empty()) {
cout << "empty" << endl;
} else {
while (!tempOutput.empty()) {
cout << tempOutput.front();
tempOutput.pop();
if (!tempOutput.empty()) {
cout << " ";
}
}
cout << endl;
}
} else {
if (inputLine.length() == 7 && inputLine[0] == '1') {
q.push(inputLine);
}
}
}
return 0;
}
#include <iostream>
#include <queue>
using namespace std;
int main() {
int n;
cin >> n; // 读取操作次数
queue<int> q; // 定义一个存储int类型元素的队列
for (int i = 0; i < n; ++i) {
int op;
cin >> op; // 读取操作类型
if (op == 1) {
// 入队操作:读取待入队的元素,加入队列
int num;
cin >> num;
q.push(num);
} else if (op == 2) {
// 出队操作:先判断队列是否为空
if (q.empty()) {
cout << "impossible!" << endl;
} else {
q.pop(); // 队首元素出队
}
} else if (op == 3) {
// 访问队首操作:先判断队列是否为空
if (q.empty()) {
cout << "impossible!" << endl;
} else {
cout << q.front() << endl; // 输出队首元素
}
}
}
return 0;
}
#include <iostream>
#include <queue>
#include <unordered_set>
using namespace std;
int main() {
int M, N;
// 输入展示架容量M和礼物数目N
cin >> M >> N;
queue<int> giftQueue; // 记录礼物摆放顺序,队首是最早摆放的礼物
unordered_set<int> giftSet; // 快速判断礼物是否已在展示架上
int happyLevel = 0; // 初始高兴程度为0
for (int i = 0; i < N; ++i) {
int currentGift;
cin >> currentGift;
// 若当前礼物不在展示架上,才需要处理摆放
if (giftSet.find(currentGift) == giftSet.end()) {
happyLevel++; // 高兴程度+1
// 若展示架已满,移除最早摆放的礼物
if (giftQueue.size() == M) {
int oldestGift = giftQueue.front();
giftQueue.pop(); // 从队列中移除最早的礼物
giftSet.erase(oldestGift); // 从集合中移除该礼物,标记为不在展示架上
}
// 将当前礼物放入展示架(队列+集合)
giftQueue.push(currentGift);
giftSet.insert(currentGift);
}
// 若礼物已存在,直接跳过,不做任何操作
}
// 输出最终的高兴程度
cout << happyLevel << endl;
return 0;
}
全部评论 3
#include <iostream>
#include <list>
#include <unordered_set>
using namespace std;int main() {
int M, N;
// 输入展示架容量M和礼物数量N
cin >> M >> N;list<int> shelf; // 存储展示架上的礼物,按放入顺序排列 unordered_set<int> exist; // 快速判断礼物是否已存在 int happy = 0; // 高兴程度 for (int i = 0; i < N; ++i) { int gift; cin >> gift; // 如果礼物已存在,跳过 if (exist.find(gift) != exist.end()) { continue; } // 礼物不存在,高兴度+1 happy++; // 如果展示架已满,移除最早放入的礼物 if (shelf.size() == M) { int oldest = shelf.front(); // 取最早放入的礼物 shelf.pop_front(); // 从展示架移除 exist.erase(oldest); // 从集合中删除 } // 放入新礼物 shelf.push_back(gift); exist.insert(gift); } // 输出最终高兴程度 cout << happy << endl; return 0;}
2026-02-04 来自 广东
0#include <iostream>
#include <queue>
using namespace std;int main() {
int n;
cin >> n; // 读取操作次数
queue<int> q; // 定义存储整数的队列for (int i = 0; i < n; ++i) { int op; cin >> op; // 读取操作类型(1/2/3) if (op == 1) { // 入队操作:读取要入队的数值并加入队列 int num; cin >> num; q.push(num); } else if (op == 2) { // 出队操作:先判断队列是否为空 if (q.empty()) { cout << "impossible!" << endl; } else { q.pop(); // 合法则弹出队首 } } else if (op == 3) { // 访问队首:先判断队列是否为空 if (q.empty()) { cout << "impossible!" << endl; } else { cout << q.front() << endl; // 输出队首元素 } } } return 0;}
2026-02-04 来自 广东
0#include <iostream>
#include <queue>
#include <string>
using namespace std;int main() {
queue<string> q; // 存储客户号码的队列
string input; // 存储每行输入while (true) { getline(cin, input); // 读取整行输入(处理带空格的out指令) // 1. 判断是否是end指令 if (input == "end") { // 处理end:输出剩余所有号码,无则不输出 if (!q.empty()) { while (!q.empty()) { cout << q.front() << " "; q.pop(); } cout << endl; } break; // 结束程序 } // 2. 判断是否是out指令(以"out "开头) else if (input.substr(0, 4) == "out ") { int k = stoi(input.substr(4)); // 提取叫号数量k int count = 0; // 记录实际叫号的数量 bool hasOutput = false; // 标记是否有输出内容 // 取出前k个(或剩余所有)号码并输出 while (count < k && !q.empty()) { if (hasOutput) { cout << " "; // 元素间加空格,开头不加 } cout << q.front(); q.pop(); count++; hasOutput = true; } // 输出结果:有内容则换行,无则输出empty if (hasOutput) { cout << endl; } else { cout << "empty" << endl; } } // 3. 否则是客户号码(7位以1开头的数字) else { // 简单验证号码格式(可选,题目保证输入合法可省略) if (input.size() == 7 && input[0] == '1') { q.push(input); // 号码入队 } } } return 0;}
2026-02-04 来自 广东
0

























有帮助,赞一个