第三章作业题3--队列

一、选择题

1、

在这里插入图片描述

答案:B


2、

在这里插入图片描述

答案:B
解析:队列是先进先出的类型,所以选B


3、

在这里插入图片描述

答案:C


4、

在这里插入图片描述

答案:C
解析:队列类头出尾入;


5、

在这里插入图片描述

答案:B


二、编程题

1、银行业务队列简单模拟

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

答案1:

#include <bits/stdc++.h>

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2


typedef int Status;
typedef int QElemType;

using namespace std;

typedef struct QNode
{
    QElemType data;
    struct QNode *next;
} QNode,*QueuePtr;

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

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

Status EnQueue(LinkQueue &A,int e)
{
    QueuePtr p;
    p=(QueuePtr)malloc(sizeof(QNode));
    if(!p) exit(OVERFLOW);
    p->data=e;
    p->next=NULL;//这里忘记了;
    A.rear->next=p;
    A.rear=p;
    return OK;
}

Status DeQueue(LinkQueue &A,int &e)
{
    if(A.front==A.rear)  return ERROR;
    e=A.front->next->data;
    QueuePtr p;
    p=A.front->next;
    A.front->next=p->next;
    if(p==A.rear) A.rear=A.front;
    free(p);
    return OK;
}
int main()
{
    LinkQueue A,B;
    InitQueue(A);
    InitQueue(B);

    int n,j;
    cin>>n;
    int j1=0,o1=0;
    int flag=0;
    for(int i=0; i<n; i++)
    {
        cin>>j;
        if(j%2!=0)
        {
            EnQueue(A,j);
            j1++;
        }
        else
        {
            EnQueue(B,j);
            o1++;
        }
    }
    int e;
    while(j1>0||o1>0)
    {
        if(j1>0&&flag==0)
        {
            DeQueue(A,e);
            printf("%d",e);j1-=1;flag=1;
            if(j1>0)
            {
                DeQueue(A,e);
                printf(" %d",e);j1-=1;
            }
        }
        else if(j1>0&&flag==1)
        {
            DeQueue(A,e);
            printf(" %d",e);j1-=1;
            flag=1;
            if(j1>0)
            {
                DeQueue(A,e);
                printf(" %d",e);j1-=1;
            }
        }
        if(o1>0&&flag==0)
        {
            DeQueue(B,e);
            printf("%d",e);o1-=1;flag=1;
        }
        else if(o1>0&&flag==1)
        {
             DeQueue(B,e);
             printf(" %d",e);
             o1-=1;
        }
    }
    printf("\n");
    return 0;
}

答案2:

#include <bits/stdc++.h>

int j[1000];
int o[1000];

int main()
{
    memset(j,0,sizeof(j));
    memset(o,0,sizeof(o));
    int n,m;
    int j1=0,o1=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&m);
        if(m%2==0)
        {
            o[o1]=m;
            o1++;
        }
        else if(m%2==1)
        {
            j[j1]=m;
            j1++;
        }
    }
    int flag=1,couj=0,couo=0;
    while(j1>0||o1>0)
    {
        if(flag==1&&j1>0)
        {
            printf("%d",j[couj]);
            couj++;
            j1--;
            flag=0;
            if(j1>0)
            {
                printf(" %d",j[couj]);
                couj++;
                j1--;
            }
        }
        else if(flag==0&&j1>0)
        {
            printf(" %d",j[couj]);
            couj++;
            j1--;
            flag=0;
            if(j1>0)
            {
                printf(" %d",j[couj]);
                couj++;
                j1--;
            }
        }
        if(flag==1&&o1>0)
        {
            printf("%d",o[couo]);
            couo++;
            o1--;
            flag=0;
        }
        else if(flag==0&&o1>0)
        {
            printf(" %d",o[couo]);
            couo++;
            o1--;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值