設某銀行有A、B兩個業務視窗,且處理業務的速度不一樣,其中A視窗處理速度是B視窗的2倍 —— 即當A視窗每處理完2個顧客時,B視窗處理完1個顧客,給定到達銀行的顧客序列,請按業務完成的順序輸出顧客序列,假定不考慮顧客先后到達的時間間隔,并且當不同視窗同時處理完2個顧客時,A視窗顧客優先輸出,
輸入格式:
輸入為一行正整數,其中第1個數字N(≤1000)為顧客總數,后面跟著N位顧客的編號,編號為奇數的顧客需要到A視窗辦理業務,為偶數的顧客則去B視窗,數字間以空格分隔,
輸出格式:
按業務處理完成的順序輸出顧客的編號,數字間以空格分隔,但最后一個編號后不能有多余的空格,
輸入樣例:
8 2 1 3 9 4 11 13 15
輸出樣例:
1 3 2 9 11 4 13 15
代碼:
import java.util.*; public class Main{ public static void main(String[] args) { Scanner scan=new Scanner(System.in); ArrayDeque<Integer> q1=new ArrayDeque<Integer>(); ArrayDeque<Integer> q2=new ArrayDeque<Integer>(); int n=scan.nextInt(); while(n-->0){ int num=scan.nextInt(); if(num%2==1) q1.offer(num); else q2.offer(num); } while(!q1.isEmpty()){ int cnt=2,k=0; while(cnt-->0 && !q1.isEmpty()){ if(k++>0) System.out.print(" "); System.out.print(q1.poll()); } if(!q2.isEmpty()){ System.out.print(" "+q2.poll()); } if(!q2.isEmpty()||!q1.isEmpty()) System.out.print(" ");//只有佇列q1或者q2不空時再加空格 } int k=0; while(!q2.isEmpty()){ if(k++>0) System.out.print(" "); System.out.print(q2.poll()); } } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/106068.html
標籤:其他
上一篇:dyt說反話(注意字串輸入)
下一篇:字串簡單操作
