1. 程式人生 > >第三章作業題3--佇列-計算機17級 7-1 銀行業務佇列簡單模擬 (25 分)

第三章作業題3--佇列-計算機17級 7-1 銀行業務佇列簡單模擬 (25 分)

7-1 銀行業務佇列簡單模擬 (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

思路:很明顯這個題在考佇列,剛開始想在一個佇列裡實現這個過程,最後發現不太現實。所以還是決定建立兩個佇列 A和B,定義兩個變數odd和even分別統計奇數和偶數個數(也就是統計A視窗和B視窗的人數)。既然是不考慮顧客先後到達的時間間隔的話,那就統一輸出就可以了。(這個題其實好懂,唯一的難點就在於要想清楚如何輸出)。

輸出方法:

  1. 當A視窗的人數大於等於2且B視窗的人數大於等於1時,輸出兩個A視窗的客戶 + 一個B視窗的客戶。
  2. 當A視窗的客戶為一個的時候且B視窗的客戶大於等於一個的時候就輸出一個A視窗的客戶 + 一個B視窗的客戶。
  3. 當A或B兩個中有一個佇列為空就退出迴圈。
  4. 然後單獨輸出那個不為空的佇列裡的客戶就可以了。

方法一:實現佇列

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

typedef int Status;
typedef int QElemType;
//建立鏈式佇列
typedef struct QNode
{
    QElemType data;
    struct QNode *next;
} QNode,*QueuePtr;

typedef struct
{
    QueuePtr front;
    QueuePtr rear;
} LinkQueue;

Status InitQueue(LinkQueue &Q)
{
    Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
    if(!Q.front)
        exit(OVERFLOW);
    Q.front->next = NULL;
    return 1;
}

Status EnQueue(LinkQueue &Q,QElemType e)
{
    QueuePtr p;
    p = (QueuePtr)malloc(sizeof(QNode));
    if(!p)
        exit(OVERFLOW);
    p->data = e;
        p->next = NULL;
    Q.rear->next = p;//尾部插入
    Q.rear = p;
    return 1;
}

Status DeQueue(LinkQueue &Q,QElemType &e)
{
    if(Q.front == Q.rear)
        return 0;
    QueuePtr p = Q.front->next;//指向隊頭
    e = p->data;
    Q.front->next = p->next;
    if(Q.rear == p)
        Q.rear = Q.front;
    free(p);
    return 1;
}

bool JudgeEmpty(LinkQueue &Q)
{
    if(Q.front == Q.rear)
        return true;
    else
        return false;
}



int main()
{
    int n;
    LinkQueue A,B;
    InitQueue(A);
    InitQueue(B);
    cin>>n;
    bool flag = true;
    int tmp,odd = 0,even = 0;//odd統計奇數,even統計偶數
    for(int i = 0; i < n; i++)
    {
        cin>>tmp;
        //A視窗統計奇數個數,B視窗統計偶數個數
        if(tmp%2==0)
        {
            EnQueue(B,tmp);
            even++;
        }
        else
        {
            EnQueue(A,tmp);
            odd++;
        }
    }
    while(odd && even)
    {
        if(odd>=2&&even>=1)
        {
            int a,b,c;
            DeQueue(A,a);
            DeQueue(A,b);
            DeQueue(B,c);
            odd-=2;
            even--;
            if(flag)
            {
                cout<<a<<" "<<b<<" "<<c;
                flag = false;
            }
            else
            {
                cout<<" "<<a<<" "<<b<<" "<<c;
            }
        }
        else if(odd == 1 && even >= 1)
        {
            int a,b;
            DeQueue(A,a);
            DeQueue(B,b);
            odd--;
            even--;
            if(flag)
            {
                cout<<a<<" "<<b;
                flag = false;
            }
            else
            {
                cout<<" "<<a<<" "<<b;
            }
        }
    }
    while(odd)
    {
        int a;
        DeQueue(A,a);
        if(flag)
        {
            cout<<a;
            flag = false;
        }
        else
        {
            cout<<" "<<a;
        }
        odd--;
    }
    while(even)
    {
        int a;
        DeQueue(B,a);
        if(flag)
        {
            cout<<a;
            flag = false;
        }
        else
        {
            cout<<" "<<a;
        }
        even--;
    }

}

方法2:STL queue 方法

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
queue<int> q1;
queue<int> q2;

int main()
{
    int n;
    cin>>n;
    bool flag = true;
    int tmp,odd = 0,even = 0;//odd統計奇數,even統計偶數
    for(int i = 0; i < n; i++)
    {
        cin>>tmp;
        //A視窗統計奇數個數,B視窗統計偶數個數
        if(tmp%2==0)
        {
            q2.push(tmp);
            even++;
        }
        else
        {
            q1.push(tmp);
            odd++;
        }
    }
    while(odd && even)
    {
        if(odd>=2&&even>=1)
        {
            int a = q1.front();
            q1.pop();
            int b = q1.front();
            q1.pop();
            int c = q2.front();
            q2.pop();
            odd-=2;
            even--;
            if(flag)
            {
                cout<<a<<" "<<b<<" "<<c;
                flag = false;
            }
            else
            {
                cout<<" "<<a<<" "<<b<<" "<<c;
            }
        }
        else if(odd == 1 && even >= 1)
        {
            int a = q1.front();
            q1.pop();
            int b = q2.front();
            q2.pop();
            odd--;
            even--;
            if(flag)
            {
                cout<<a<<" "<<b;
                flag = false;
            }
            else
            {
                cout<<" "<<a<<" "<<b;
            }
        }
    }
    while(odd)
    {
        int a = q1.front();
        q1.pop();
        if(flag)
        {
            cout<<a;
            flag = false;
        }
        else
        {
            cout<<" "<<a;
        }
        odd--;
    }
    while(even)
    {
        int a = q2.front();
        q2.pop();
        if(flag)
        {
            cout<<a;
            flag = false;
        }
        else
        {
            cout<<" "<<a;
        }
        even--;
    }

}