7-6 银行业务队列简单模拟 (5分) JAVA解法

本文介绍了一种使用Java实现的客户处理方案,通过创建两个队列分别存储奇数和偶数编号的客户,利用Queue.offer(), Queue.poll()和Queue.peek()方法进行入队、出队和查看队首元素的操作,确保了客户处理的高效性和有序性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述
思路:定义两个队列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();
	    		}
	    	}
	    }
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jay_fearless

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值