#include<malloc.h>
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等
typedef int QElemType;
#define MAXQSIZE 100 // 最大队列长度(对于循环队列,最大队列长度要减1)
typedef struct
{
QElemType *base; // 初始化的动态分配存储空间
int front; // 头指针,若队列不空,指向队列头元素
int rear; // 尾指针,若队列不空,指向队列尾元素的下一个位置
}SqQueue;
Status InitQueue(SqQueue &Q)
{
// 构造一个空队列Q,该队列预定义大小为MAXQSIZE
Q.base = (QElemType*)malloc(MAXQSIZE * sizeof(QElemType));
if (!Q.base) exit(1);
Q.rear = Q.front = 0;
return OK;
}
Status EnQueue(SqQueue &Q, QElemType e)
{
// 插入元素e为Q的新的队尾元素
if ((Q.rear + 1) % MAXQSIZE == Q.front) return ERROR;
Q.base[Q.rear] = e;
Q.rear = (Q.rear + 1) % MAXQSIZE;
return OK;
}
Status DeQueue(SqQueue &Q, QElemType &e)
{
// 若队列不空, 则删除Q的队头元素, 用e返回其值, 并返回OK; 否则返回ERROR
if (Q.front == Q.rear) return ERROR;
e = Q.base[Q.front];
Q.front = (Q.front + 1) % MAXQSIZE;
return OK;
}
Status GetHead(SqQueue Q, QElemType &e)
{
// 若队列不空,则用e返回队头元素,并返回OK,否则返回ERROR
if (Q.front == Q.rear) return ERROR;
e = Q.base[Q.front];
return OK;
}
int QueueLength(SqQueue Q)
{
// 返回Q的元素个数
return Q.rear%MAXQSIZE - Q.front%MAXQSIZE;
}
int main() {
int i, b[50], e[50],wait=0,mid=0;
float time = 0;
QElemType dele;
SqQueue Q;
//float a;
InitQueue(Q);
scanf("%d", &i);//ren_num
for (int j = 0; j < i; j++) {
scanf("%d %d",&b[j],&e[j]);//到达时刻 办理时长
}
mid = b[0];
for (int j = 1; j < i; j++) {
mid += e[j - 1];//mid为上一个顾客办理业务结束时间
if (mid> b[j]) {
wait = mid - b[j];
EnQueue(Q, mid - b[j]);
}
else {
mid = b[j];//当顾客到的时间大于上一个顾客结束时间 那么当前顾客为重新计时点
}
}
int num = QueueLength(Q) ;
for (int j = 0; j < num; j++) {
DeQueue(Q, mid);
time += mid;
}
time =time/i;
printf("%0.2f", time);
return 0;
}/*1.可能顾客不用排队 到的时间大于前面顾客已经办完事情的时长-需要判断
2.队列装的是等待时长
*/
tip:判断是否上一个顾客结束业务时间 大于 当前顾客到达时间!!!