
思路:定义两个队列A,B分别用来存储奇数号的客户和偶数号的客户,然后用Queue.offer()让用户入队,用Queue.poll()让用户出队,用Queue.peek()输出队首元素。

//尽量使用offer()方法添加元素,使用poll()方法移除元素。dd()和remove()方法在失败的时候会抛出异常。
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
Queue<Integer> a= new LinkedList<>();;
Queue<Integer> b= new LinkedList<>();;
for(int i=0;i<n;i++)
{
int num = cin.nextInt();
if(num%2==1) a.offer(num); //
else if(num%2==0) b.offer(num);
}
int flag=0;
while(!a.isEmpty() || !b.isEmpty())
{
if(!a.isEmpty()) {
if(flag==0)
{
System.out.print(a.peek());
a.poll();
flag=1;
}
else {
System.out.print(" "+a.peek());
a.poll();
}
}
if(!a.isEmpty()) {
System.out.print(" "+a.peek());
a.poll();
}
if(!b.isEmpty()) {
if(flag==0)
{
System.out.print(b.peek());
b.poll();
flag=1;
}
else {
System.out.print(" "+b.peek());
b.poll();
}
}
}
}
}
本文介绍了一种使用Java实现的客户处理方案,通过创建两个队列分别存储奇数和偶数编号的客户,利用Queue.offer(), Queue.poll()和Queue.peek()方法进行入队、出队和查看队首元素的操作,确保了客户处理的高效性和有序性。


被折叠的 条评论
为什么被折叠?



