队列的基本操作
本题重新定义队列出队的操作:队首出队的数字重新在队尾入队。例:队列中有1 2 3三个数字,现要求队首出队,则1从队首出队,同时1从队尾入队,队列变成2 3 1。 入队的顺序为1,2,3,4…n,同时给一个二进制字符串,1代表出队操作,0代表入队操作。
输入格式:
在第一行有两个数字n,m(n<=100,n<m),其中n为入队的数字个数,m代表操作数。接下来m行,每行一个数字,1或者0,代表不同的操作。
输出格式:
输出操作后队列的每个数字,数字间以空格分隔,最后一个数字后没有空格。
输入样例:
5 8
0
0
1
0
1
0
1
0
输出样例:
在这里给出相应的输出。例如:
3 2 4 1 5
代码如下:
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a=input.nextInt();
Deque<Integer> deque = new LinkedList<>();
Queue<Integer> queue=new LinkedList<>();
for (int i=1;i<=a;i++)
queue.add(i);
int b=input.nextInt();
for (int i=0;i<b;i++){
int c=input.nextInt();
if (c==0){
deque.add(queue.poll());
}else {
deque.addLast(deque.getFirst());
deque.removeFirst();
}
}
int last=deque.pollLast();
for (Integer i:deque)
System.out.print(i+" ");
System.out.print(last);
}
}