这是原题:
有3个队列A、队列B、队列C,原队列长度分别为10人、13人、16人。在5秒钟间隔内有1人到达,他以排队的规则是:哪个队列越短,他选中该队列的概率就越大;每个人离队的规律是:在6秒钟内,以概率P离队。当离队概率P=0.45时,计算500秒内队列A、队列B、队列C的长度分别是多少?
这是我的程序代码:
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXQSIZE 1000
typedef int status;
typedef struct Node
{
int front;//头指针,指向队头元素
int rear;//尾指针,指向队尾元素的下一个位置
}Queue;//队列的顺序存储结构
status InitQueue(Queue Q)
{
Q=(Queue)malloc(sizeof(Node));
(*Q).front=(*Q).rear=0;
return OK;
}//队列初始化
int QueueLength(Queue Q)
{
return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
}//返回Q的长度
status EnQueue(Queue *Q)
{
if(((*Q).rear+1)%MAXQSIZE==(*Q).front)//队列满
return ERROR;
(*Q).rear=((*Q).rear+1)%MAXQSIZE;
return OK;
}//入队
status DeQueue(Queue *Q)
{
if((*Q).front==(*Q).rear)
return ERROR;
(*Q).front=((*Q).front+1)%MAXQSIZE;
return OK;
}//出队
Queue Minimun(Queue A,Queue B,Queue C)
{
int l1=QueueLength(A);
int l2=QueueLength(B);
int l3=QueueLength(C);
if(l1<=l2&&l1<=l3)
return A;
if(l2<=l1&&l2<=l3)
return B;
if(l3<=l2&&l3<=l1)
return C;
}//返回最短队列
int main()
{
while(1)
{
int t;float P;
Queue A;
Queue B;
Queue C;
InitQueue(&A);
InitQueue(&B);
InitQueue(&C);
for(int i=1;i<=10;i++)
EnQueue(&A);
for(int i=1;i<=13;i++)
EnQueue(&B);
for(int i=1;i<=16;i++)
EnQueue(&C);
printf("请输入离队概率(以0到1之间的小数表示,输入0退出):");
scanf("%f",&P);
if(P==0)
{
printf("结束退出......\n");
exit(-1);
}
P=P*100;
printf("请输入时长:");
scanf("%d",&t);
int c=0;
while(++c<=t)
{
if(c%5==0)
{
Queue Q=Minimun(A,B,C);
EnQueue(&Q);
}
if(c%6==0)
{
int r1=rand()%101;
int r2=rand()%101;
int r3=rand()%101;
if(r1<P)
DeQueue(&A);
if(r2<P)
DeQueue(&B);
if(r3<P)
DeQueue(&C);
}
}
printf("A:%d\nB:%d\nC:%d\n\n",QueueLength(A),QueueLength(B),QueueLength(C));
}
return 0;
}
结果出来是这个样子的:
但是正确的结果应该是这样的:
我不知道哪里出问题了...