PTA 银行业务队列简单模拟

本文介绍了一个简单的银行业务队列模拟程序,通过该程序可以模拟银行A、B两个业务窗口处理顾客请求的过程。顾客按奇偶编号分别进入不同的窗口排队,程序输出顾客按业务完成的顺序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

银行业务队列简单模拟

设某银行有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


Code:

#include<stdio.h>
#include<stdlib.h>

struct queue {
    int Data[1000];
    int Rear;  //队尾 
    int Front; //队头 
};

void Addqueue(struct queue *q, int x) //入队
{
    q-> Rear++;
    q->Data[q->Rear] = x;
}

int Deletequeue(struct queue *q) //出队
{
    q->Front ++;

    return q->Data[q->Front];
}

void Createqueue(struct queue *q) //初始化队列
{
    q->Rear = -1;
    q->Front = -1;
}

int Isqueue(struct queue *q) //判断队列是否队空
{
    int i;
    if(q->Rear == q->Front) {
        i = 0;
    }
    else {
        i = 1;
    }

    return i;
} 

int main(void)
{
    struct queue QA, QB;
    Createqueue(&QA);
    Createqueue(&QB);
    int i, n, x;
    scanf("%d", &n);
    for(i = 0; i < n; i ++) {
        scanf("%d", &x);
        if(x % 2 != 0) {
            Addqueue(&QA, x);
        }
        else {
            Addqueue(&QB, x);
        }
    }
    i = 0;
    while(Isqueue(&QA) || Isqueue(&QB)) {
        if(Isqueue(&QA)) {
            if(i == 0) {
                printf("%d", Deletequeue(&QA));
                i ++;
            }
            else {
                printf(" %d", Deletequeue(&QA));
            }   
        }
        if(Isqueue(&QA)) {
            printf(" %d", Deletequeue(&QA));
        }
        if(Isqueue(&QB)) {
            if(i == 0) {
                printf("%d", Deletequeue(&QB));
                i ++; 
            }
            else {
                printf(" %d", Deletequeue(&QB));
            }
        }
    }

    return 0;
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值