First Element of a Queue
Description
You are given a queue that contains a series of integers. Your task is to access the first element of the queue and print it. 给定一个包含一系列整数的队列,您的任务是访问队列中的第一个元素并打印它
Input
The input will contain: The first line will contain an integer n (1 ≤ n ≤ 100), the number of elements in the queue. The next n lines will each contain an integer to enqueue into the queue. 输入将包含: 第一行将包含一个整数 n(1 ≤ n ≤ 100),表示队列中元素的数量。 接下来的 n 行将包含要入队的整数。
Output
Print the first element of the queue. 打印队列中的第一个元素。
Sample Input 1
3 10 20 30
Sample Output 1
10
#include <iostream>
#include <queue>
int main() {
int n;
std::cin >> n;
std::queue<int> q;
for (int i = 0; i < n; ++i) {
int num;
std::cin >> num;
q.push(num);
}
if (!q.empty()) {
std::cout << q.front() << std::endl;
}
return 0;
}
Access the Back Element
Description
You are given a queue that contains a series of integers. Your task is to access the last element of the queue and print it. 给定一个包含一系列整数的队列,您的任务是访问队列中的最后一个元素并打印它。
Input
The input will contain: The first line will contain an integer n (1 ≤ n ≤ 100), the number of elements in the queue. The next n lines will each contain an integer to enqueue into the queue. 输入将包含: 第一行将包含一个整数 n(1 ≤ n ≤ 100),表示队列中元素的数量。 接下来的 n 行将包含要入队的整数。
Output
Print the last element of the queue. 打印队列中的最后一个元素。
Sample Input 1
3 10 20 30
Sample Output 1
30
#include <iostream>
#include <queue>
int main() {
int n;
std::cin >> n;
std::queue<int> q;
for (int i = 0; i < n; ++i) {
int num;
std::cin >> num;
q.push(num);
}
while (q.size() > 1) {
q.pop();
}
if (!q.empty()) {
std::cout << q.front() << std::endl;
}
return 0;
}
Check if the Queue is Empty
Description
You are given a queue. Your task is to check if the queue is empty. Print "YES" if the queue is empty, and "NO" if it is not empty. 给定一个队列,您的任务是检查队列是否为空。如果队列为空,打印 "YES";如果队列不为空,打印 "NO"。
Input
The input will contain: The first line will contain an integer n (1 ≤ n ≤ 100), the number of elements in the queue. The next n lines will each contain an integer to enqueue into the queue. 输入将包含: 第一行将包含一个整数 n(1 ≤ n ≤ 100),表示队列中元素的数量。 接下来的 n 行将包含要入队的整数。
Output
Print "YES" if the queue is empty after the operations, otherwise print "NO". 如果操作后队列为空,打印 "YES";否则,打印 "NO"。
Sample Input 1
3 10 20 30
Sample Output 1
NO
#include <iostream>
#include <queue>
using namespace std;
int main() {
int n;
cin >> n;
queue<int> q;
for (int i = 0; i < n; ++i) {
int num;
cin >> num;
q.push(num);
}
if (q.empty()) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
Enqueue Queue
Description
You are given a queue that starts empty. Your task is to simply enqueue a series of integers into the queue. After the enqueue operations, print the elements in the queue from front to back in the same order they were added. 给定一个空队列,您的任务是将一系列整数入队。入队操作完成后,按添加的顺序从前到后打印队列中的元素。
Input
The input will contain: The first line will contain an integer n (1 ≤ n ≤ 100), the number of elements to enqueue. The next n lines will each contain an integer to enqueue into the queue.
输入将包含: 第一行将包含一个整数 n(1 ≤ n ≤ 100),表示要入队的元素数量。 接下来的 n 行将包含要入队的整数。
Output
Print all the elements in the queue, from front to back, in the same order they were added. Each element should be printed on the same line, separated by a space. 打印队列中所有的元素,从前到后,按添加的顺序。每个元素应该打印在同一行,元素之间用空格分隔。
Sample Input 1
3 10 20 30
Sample Output 1
10 20 30
#include <iostream>
#include <queue>
int main() {
int n;
std::cin >> n;
std::queue<int> q;
for (int i = 0; i < n; ++i) {
int num;
std::cin >> num;
q.push(num);
}
while (!q.empty()) {
std::cout << q.front();
q.pop();
if (!q.empty()) {
std::cout << " ";
}
}
return 0;
}
Remove One Element from the Queue
Description
You are given a queue that contains a series of integers. Your task is to remove the front element of the queue, and then print the remaining elements in the queue from front to back. 给定一个包含一系列整数的队列,您的任务是删除队列中的第一个元素,然后打印队列中剩余的元素,从前到后。
Input
The input will contain: The first line will contain an integer n (1 ≤ n ≤ 100), the number of elements in the queue. The next n lines will each contain an integer to enqueue into the queue. 输入将包含: 第一行将包含一个整数 n(1 ≤ n ≤ 100),表示队列中元素的数量。 接下来的 n 行将包含要入队的整数。
Output
After removing the front element from the queue, print all the remaining elements in the queue from front to back, separated by spaces. If the queue becomes empty, print nothing. 删除队列中的第一个元素后,打印剩余的队列元素,从前到后,元素之间用空格分隔。如果队列为空,则不打印任何内容。
Sample Input 1
3 10 20 30
Sample Output 1
20 30
n = int(input())
queue = []
for _ in range(n):
num = int(input())
queue.append(num)
if len(queue) > 0:
queue.pop(0)
print(' '.join(map(str, queue)))
标签:TLS,will,题目,int,queue,队列,num,B1,contain
From: https://blog.csdn.net/2301_78398877/article/details/145071586