zoukankan      html  css  js  c++  java
  • 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窗口的客户,并分别统计两个窗口的人数。既然是不考虑顾客先后到达的时间间隔的话,那就统一输出就可以了。

    1. 输出两个A窗口的客户 + 一个B窗口的客户。
    2. 当A窗口的客户为一个的时候且B窗口的客户大于等于一个的时候就输出一个A窗口的客户 + 一个B窗口的客户。
    3. 当A或B两个中有一个队列为空就退出循环。
    4. 然后单独输出那个不为空的队列里的客户就可以了。

    一开始把这个题想的太复杂了,后来仔细考虑了一下题中给出的条件,发现其实挺简单的。

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const ll MOD = 2147493647;
    const int maxn = 1e5+10;
    typedef struct QNode {
        int data;
        struct QNode *next;
    } QNode,*QueuePtr;
    
    typedef struct {
        QueuePtr frot;
        QueuePtr rear;
    } LinkQueue;
    
    bool initQueue(LinkQueue& Q) {
        Q.frot = Q.rear = (QueuePtr)malloc(sizeof(QNode));
        if(!Q.frot)
            exit(-2);
        Q.frot->next = NULL;
        return true;
    }
    
    bool DestroyQueue(LinkQueue& Q) {
        while(Q.frot) {
            Q.rear = Q.frot->next;
            free(Q.frot);
            Q.frot = Q.rear;
        }
        return true;
    }
    
    bool EnQueue(LinkQueue& Q, int e) {
        QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
        if(p==NULL) {
            exit(-2);
        }
        p->data = e;
        p->next = NULL;
        Q.rear->next = p;
        Q.rear = p;
        return true;
    }
    
    bool DeQueue(LinkQueue& Q,int& e) {
        if(Q.frot==Q.rear)
            return false;
        QueuePtr p = Q.frot->next;
        e = p->data;
        Q.frot->next = p->next;
        if(Q.rear == p)
            Q.rear == Q.frot;
        free(p);
        return true;
    }
    
    bool isEmpty(LinkQueue& Q){
        if(Q.frot==Q.rear){
            return true;
        }
        return false;
    }
    
    
    int main() {
        int n;
        LinkQueue A,B;
        initQueue(A);
        initQueue(B);
        int even = 0,odd = 0,tmp;
        bool isfirst = true;
        cin>>n;
        for(int kk = 0; kk<n; kk++) {
            cin>>tmp;
            if(tmp&1) {
                EnQueue(A,tmp);
                even++;
            } else {
                EnQueue(B,tmp);
                odd++;
            }
        }
        while(even&&odd){
            if(even>=2 && odd>=1){
                int a,b,c;
                DeQueue(A,a);
                DeQueue(A,b);
                DeQueue(B,c);
                even-=2;
                odd--;
                if(isfirst){
                    cout<<a<<" "<<b<<" "<<c;
                    isfirst = false;
                }
                else
                    cout<<" "<<a<<" "<<b<<" "<<c;
            }
            else if(even==1 && odd>=1){
                int a,b;
                DeQueue(A,a);
                DeQueue(B,b);
                even--;
                odd--;
                if(isfirst){
                    cout<<a<<" "<<b;
                    isfirst = false;
                }
                else{
                    cout<<" "<<a<<" "<<b;
                }
            }
        }
        while(even){
            int a;
            DeQueue(A,a);
            if(isfirst){
                cout<<a;
                isfirst = false;
            }
            else{
                cout<<" "<<a;
            }
            even--;
        }
        while(odd){
            int a;
            DeQueue(B,a);
            if(isfirst){
                cout<<a;
                isfirst = false;
            }
            else{
                cout<<" "<<a;
            }
            odd--;
        }
        return 0;
    }
    /*
    样例输入:
    8 2 1 3 9 4 11 13 15
    样例输出:
    1 3 2 9 11 4 13 15
    */
    View Code
  • 相关阅读:
    12.1
    11.26
    12.5Java日报
    11.25
    11.27
    12.03
    11.28
    12.04
    如何在TortoiseGit中使用sshkeygen生成的key
    leetcode 39 组合总和
  • 原文地址:https://www.cnblogs.com/sykline/p/9784466.html
Copyright © 2011-2022 走看看