一、判断题
1、
答案:F
解析:队列的特点 first in first out,因此F
2、
答案:F
解析:数组表示的循环队列每次插入时候如果不满,则rear = (rear + 1) % size, 因此 rear 完全可能小于front。
3、
答案:T
二、选择题
1、
答案:A
解析:删除了两个,所以front=(front+2)%MAXQSIZE=(0+2)%6=2;
加入了两个,所以rear=(rear+2)%MAXQSIZE=(4+2)%6=0
2、
答案:B
3、
答案:D
解析:size=(rear-front+MAXQSIZE)%MAXQSIZE
注意这个题问的是队尾元素的位置而不是队尾指针,队尾指针的位置是队尾元素的下一个。
队尾元素位置不同于队尾下标,队尾下标是队尾元素位置的下一个位置。而队尾下标:size = rear - front。
三、 编程题
1、银行排队问题之单队列多窗口服务
假设银行有K个窗口提供服务,窗口前设一条黄线,所有顾客按到达时间在黄线后排成一条长龙。当有窗口空闲时,下一位顾客即去该窗口处理事务。当有多个窗口可选择时,假设顾客总是选择编号最小的窗口。
本题要求输出前来等待服务的N位顾客的平均等待时间、最长等待时间、最后完成时间,并且统计每个窗口服务了多少名顾客。
输入格式:
输入第1行给出正整数N(≤1000),为顾客总人数;随后N行,每行给出一位顾客的到达时间T和事务处理时间P,并且假设输入数据已经按到达时间先后排好了顺序;最后一行给出正整数K(≤10),为开设的营业窗口数。这里假设每位顾客事务被处理的最长时间为60分钟。
输出格式:
在第一行中输出平均等待时间(输出到小数点后1位)、最长等待时间、最后完成时间,之间用1个空格分隔,行末不能有多余空格。
在第二行中按编号递增顺序输出每个窗口服务了多少名顾客,数字之间用1个空格分隔,行末不能有多余空格。
输入样例:
9
0 20
1 15
1 61
2 10
10 5
10 3
30 18
31 25
31 2
3
输出样例:
6.2 17 61
5 3 1
答案:
#include <bits/stdc++.h>
typedef struct Node
{
int arrive,deal;
} Node;
Node q[1005];
int main()
{
int front=0;//队列的头部和队列的尾部为0;
int rear=0;
int n;
scanf("%d",&n);
for(int i=0; i<n; i++)
{
scanf("%d%d",&q[rear].arrive,&q[rear].deal);
if(q[rear].deal>60) q[rear].deal=60;
rear+=1;
}
int window;
scanf("%d",&window);
int sumwait=0,lenthesttime=0,waittime=0;
int finish[15]= {0},numberwait[15]= {0}; //不同窗口完成一个人之后的时间、窗口人数;
while(front<rear)
{
int flag=0,minwait=99999,mindex=0;//判断是否等待的变量;
for(int i=0; i<window; i++)
{
if(finish[i]<=q[front].arrive)//如果队列首位的到达时间比完成时间晚的话,就不需要等待;
//如果队列首位,到达时间比完成时间大,就代表不需要等待
/*===== (由于数据做了修改,这里讲<改为<=)=======*/
{
finish[i]=q[front].arrive+q[front].deal;//更新这个窗口完成这次业务之后的时间:
numberwait[i]+=1;//对应的窗口人数加1;
flag=1;
front+=1;
break;
}
if(minwait>finish[i])
{
minwait=finish[i];
mindex=i;
}
}
if(!flag)//如果需要等待
{
waittime=minwait-q[front].arrive;//需要等待的时间=最少需要等待的时间-到达的时间;
if(lenthesttime<waittime) lenthesttime=waittime;
sumwait+=waittime;
finish[mindex]=minwait+q[front].deal;//等待的这个人完成业务之后的时间=等待的时间+完成业务需要的时间;
numberwait[mindex]+=1;
front+=1;//删除队列的首位;
}
}
int lasttime=0;
for(int i=0; i<window; i++)
{
if(lasttime<finish[i]) lasttime=finish[i];
}
printf("%.1lf %d %d\n",1.0*sumwait/n,lenthesttime,lasttime);
for(int i=0; i<window; i++)
{
if(i==0)
printf("%d",numberwait[i]);
else printf(" %d",numberwait[i]);
}
printf("\n");
return 0;
}
四、列车调度
输入格式:
输入第一行给出一个整数N (2 ≤ N ≤105),下一行给出从1到N的整数序号的一个重排列。数字间以空格分隔。
输出格式:
在一行中输出可以将输入的列车按序号递减的顺序调离所需要的最少的铁轨条数。
输入样例:
9
8 4 2 5 3 9 1 6 7
输出样例:
4
答案:
链表方式 正确代码(必须用二分法,否则会超时)
#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;
typedef struct QNode
{
QElemType data;
QNode *next;
} QNode,*QueuePtr;
typedef struct
{
QueuePtr front;
QueuePtr rear;
} LinkQueue;
using namespace std;
Status InitQueue(LinkQueue &Q)
{
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q.front) exit(OVERFLOW);
Q.front->next=NULL;
return OK;
}
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 OK;
}
Status GetRear(LinkQueue Q,QElemType &e1)
{
if(Q.rear==Q.front) return ERROR;
e1=Q.rear->data;
return OK;
}
bool CompareQueue(QElemType e1,QElemType e)
{
if(e1>e) return true;
else return false;
}
int main()
{
LinkQueue Q[100010];
int n,e,cou=0,e1;
cin>>n;
for(int i=0; i<n; i++)
{
cin>>e;
if(cou>0)
{
GetRear(Q[cou-1],e1);
}
if(cou==0||!CompareQueue(e1,e))
{
InitQueue(Q[cou]);
EnQueue(Q[cou],e);
cou+=1;
}
else
{
int l=0,r=cou-1;
while(l<r)
{
int mid=(l+r)/2;
GetRear(Q[mid],e1);
if(e1>e) r=mid;
else l=mid+1;
}
EnQueue(Q[l],e);
}
}
printf("%d\n",cou);
return 0;
}
链表方式 超时答案
#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;
typedef struct QNode
{
QElemType data;
QNode *next;
} QNode,*QueuePtr;
typedef struct
{
QueuePtr front;
QueuePtr rear;
} LinkQueue;
using namespace std;
Status InitQueue(LinkQueue &Q)
{
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q.front) exit(OVERFLOW);
Q.front->next=NULL;
return OK;
}
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 OK;
}
Status GetRear(LinkQueue Q,QElemType &e1)
{
if(Q.rear==Q.front) return ERROR;
e1=Q.rear->data;
return OK;
}
bool CompareQueue(QElemType e1,QElemType e)
{
if(e1>e) return true;
else return false;
}
int main()
{
LinkQueue Q[100010];
int n,e,cou=1,e1;
cin>>n;
int cou1=0;
for(int i=0; i<n; i++)
{
if(cou1==0)
{
InitQueue(Q[cou]);
cin>>e;
EnQueue(Q[cou],e);
cou1=1;
}
else
{
int flag=0;
cin>>e;
for(int j=1; j<cou+1; j++)
{
GetRear(Q[j],e1);
if(CompareQueue(e1,e))
{
EnQueue(Q[j],e);
flag=1;
break;
}
}
if(flag==0)
{
cou+=1;
InitQueue(Q[cou]);
EnQueue(Q[cou],e);
}
}
}
printf("%d\n",cou);
return 0;
}
数组方式 正确答案
#include <bits/stdc++.h>
int main()
{
int n;
scanf("%d",&n);
int a[100010];
int len=0;
for(int i=0;i<n;i++)
{
int e;scanf("%d",&e);
if(len==0||a[len-1]<e)
{
a[len]=e;
len+=1;
}
else
{
int l=0,r=len-1;
while(l<r)
{
int mid=(l+r)/2;
if(a[mid]<e) l=mid+1;
else r=mid;
}
a[l]=e;
}
}
printf("%d\n",len);
return 0;
}