1. 程式人生 > >7-5 銀行業務佇列簡單模擬 (25 分)

7-5 銀行業務佇列簡單模擬 (25 分)

7-5 銀行業務佇列簡單模擬 (25 分)

設某銀行有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
#include<bits/stdc++.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
using namespace std;
typedef long long ll;
struct LinkQueue{
    int *base;
    int front,last;
};
void InitQueue(LinkQueue& q){
    q.base = (int*)malloc(1007*sizeof(int));
    q.front = q.last = 0;
}
void push(LinkQueue&q,int e){
    q.base[q.last++] = e;
}
void pop(LinkQueue&q){
    q.front++;
}
int GetTop(LinkQueue&q){
    return q.base[q.front];
}
int EmptyQueue(LinkQueue&q){
    if(q.front==q.last) return true;
    return false;
}
int main()
{
    //freopen("i.txt","r",stdin);
    int n;
    LinkQueue q1,q2;
    scanf("%d",&n);
    InitQueue(q1);InitQueue(q2);
    for(int i=1;i<=n;i++){
        int e;
        scanf("%d",&e);
        if(e&1){
            push(q1,e);
        }else push(q2,e);
    }
    int i=0;
    while(!EmptyQueue(q1)||!EmptyQueue(q2)){
        if(!EmptyQueue(q1)){
            if(i++) printf(" ");
            cout<<GetTop(q1);
            pop(q1);
        }
        if(!EmptyQueue(q1)){
            if(i++) printf(" ");
            cout<<GetTop(q1);
            pop(q1);
        }
        if(!EmptyQueue(q2)){
            if(i++) printf(" ");
            cout<<GetTop(q2);
            pop(q2);
        }
    }
}