Easy - STL-queue
给定一个初始为空的队列,执行下面几种操作n次:
1 x :表示在队列的末尾添加数字x x(1 ≤ x ≤ 100)。
2 :输出队首的数字。
3 :删除队首的数字(保证此时数组不为空)。
4 :输出队列中含有的数字个数。
Input
第一行,一个整数n(1 ≤ n ≤ 20),表示操作的次数。
第2到n+1行,表示每一种操作。
Output
每次操作如果需要输出则输出一行数字。
Sample 1
Inputcopy | Outputcopy |
---|---|
7 1 7 2 1 6 2 3 4 2 |
7 7 1 6 |
#include <iostream>
#include <queue>
using namespace std;
int main()
{
int n;
cin >>n;
queue<int> q;
while (n--)
{
int n;
cin >>n;
if (n==1)
{
int a;
cin >>a;
q.push(a);
}
else if (n==2)
{
if (!q.empty()) cout << q.front() << endl;
}
else if (n==3)
{
if (!q.empty()) q.pop();
}
else if (n==4)
{
cout << q.size() << endl;
}
}
}