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

// 重新做的一个高级一点的写法,typedef 是C++的关键字吗??????
#include<stdio.h>
#include<iostream>
#define Maxn 100000
#define OverFlow -2
#define OK 1
#define ERROR -1
using namespace std;
typedef struct  {
    int *base;
    int front,rear;
}Sq;

int InitQueue(Sq &q) {//  队列初始化
    q.base = new int[Maxn];// MaxSize 应该不会爆栈
    if(!q.base) return OverFlow;
    q.front = q.rear = 0;
    return OK;
}

int DeQueue(Sq &q,int &e) {
    if(q.front == q.rear) return ERROR;
    e = q.base[q.front++]; // 取队头元素
    return OK;
}

inline void Print(int *arr,int n) {
    printf("%d",arr[0]);
    for(int i=1; i<n; i++) printf(" %d",arr[i]);
}
int result[Maxn];
int main(int argc,char* agrv[]) {
    Sq A,B;
    InitQueue(A); InitQueue(B);
    int N,data;
    scanf("%d",&N);
    for(int i=1; i<=N; i++) {
        scanf("%d",&data);
        if(data & 1) {// 奇数去A窗口办理业务
            A.base[A.rear++] = data;
        }
        else {
            B.base[B.rear++] = data;
        }
    }
    // cout << A.front << ' ' << A.rear << endl;
    // cout << B.front << ' ' << B.rear << endl;

    //while()
    //Print(A.base,A.rear - A.front);
    int cnt = 0,tmp;
    while(A.front != A.rear && B.front != B.rear) {
        DeQueue(A,tmp);
        result[cnt++] = tmp;
        DeQueue(A,tmp);
        result[cnt++] = tmp;
        DeQueue(B,tmp);
        result[cnt++] = tmp;
    }
    while(A.rear != A.front) {
        DeQueue(A,tmp);
        result[cnt++] = tmp;
    }
    while(B.rear != B.front) {
        DeQueue(B,tmp);
        result[cnt++] = tmp;
    }
    Print(result,N);
    //system("pause");
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

七情六欲·

学生党不容易~

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

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

打赏作者

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

抵扣说明:

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

余额充值