您的位置:首页 > 理论基础 > 数据结构算法

数据结构上机题目--离散时间模拟(银行等待问题)

2013-10-28 16:32 489 查看
/*************************************************************************
* author:crazy_石头
* algorithm:纯纯的模拟
* date:2013/09/29
* 程序功能:模拟银行等待时间....orz
**************************************************************************/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <time.h>
#include <stack>
#include <string>
#include <windows.h>
#include <algorithm>

using namespace std;

#define B (Head)malloc(sizeof(HEAD))
#define N 100005
#define M 400010
#define INF INT_MAX
const int maxn=5;

typedef struct Node
{
    int Arrive;//到达时间;
    int wait;//等待时间;
    int leave;//离开时间=办理时间+等待时间+到达时间;
    int work;//办理业务的时间;
    int customer;//当前队伍中的人数;
    struct Node *next;
}Node;

typedef struct
{
    int num;
    int sum;
    int total;
    Node  *next;
}HEAD,*Head;

inline void init(Head q)
{
    q->sum=0;
    q->total=0;
    q->next=NULL;
}

inline Head cmp(Head p,Head q)
{
    if(p->sum<=q->sum)
        return p;
    else
        return q;
}

inline void Dequeue(Head head)
{
    Node *p,*q;
    p=head->next;
    q=head->next->next;
    head->next=q;

    printf("出队:窗口号=%d  客户编号=%d  到达时间=%d 等待时间=%d 工作时间=%d 离开时间=%d\n\n",
           head->num,p->customer,p->Arrive,p->wait,p->work,p->leave);

    head->sum--;
    head->total+=p->leave-p->Arrive;
    free(p);//让head后紧跟的结点出队;
}

inline void Enqueue(Head head,Node *A)//获得队尾元素;
{
    Node *p,*q;
    if(head->next==NULL)
    {
        A->wait=0;
        A->leave=A->Arrive+A->work;
        head->next=A;//当前窗口没有人时直接去办理业务即可,无需等待;
    }
    else
    {
        p=head->next;
        while(p->next!=NULL)
            p=p->next;//找到队尾;
        A->wait=p->leave-A->Arrive;
        A->leave=A->wait+A->Arrive+A->work;
        p->next=A;
    }
    head->sum++;//人数增加;
}

inline void Leave(Head p,Head q,Head r,Node *s)
{
    if(p->next!=NULL&&p->next->leave<=s->Arrive)
    {
        Dequeue(p);
    }
    if(q->next!=NULL&&q->next->leave<=s->Arrive)
    {
        Dequeue(q);
    }
    if(r->next!=NULL&&r->next->leave<=s->Arrive)
    {
        Dequeue(r);
    }
}

inline void Insert(Head p,Head q,Head r,Node *s)
{
    Head h;
    h=cmp(cmp(p,q),r);
    Enqueue(h,s);
}

inline void print(Head head)
{
    Node *p;
    printf("\n打印信息\n\n");
    if(head->next==NULL)
        printf("%d  空队列\n",head->num);
    else
    {
        p=head->next;
        while(p!=NULL)
        {
            printf("客户编号=%d  到达时间=%d  等待时间=%d  工作时间=%d  离开时间=%d\n",
                p->customer,p->Arrive,p->wait,p->work,p->leave);
            p=p->next;
        }
    }
    printf("\n打印完毕!\n");
}

inline void PrintAll(Head p,Head q,Head r)
{
    printf("1号窗口: 总时间=%d\n",p->total);
    print(p);
    printf("2号窗口: 总时间=%d\n",q->total);
    print(q);
    printf("3号窗口: 总时间=%d\n",r->total);
    print(r);
    printf("\n\n");
}

inline void Handle(Head p,Head q,Head r,Node *A)
{
    Leave(p,q,r,A);
    if(p->next==NULL)
    {
        Enqueue(p,A);
    }
    else if(q->next==NULL)
    {
        Enqueue(q,A);
    }
    else if(r->next==NULL)
    {
        Enqueue(r,A);
    }
    else
    {
        Leave(p,q,r,A);
        Insert(p,q,r,A);
    }

}

inline void solve(Head p,Head q,Head r)
{
    while(p->next!=NULL)
    {
        Dequeue(p);
    }
    while(q->next!=NULL)
    {
        Dequeue(q);
    }
    while(r->next!=NULL)
    {
        Dequeue(r);
    }
}

int main()
{
    Head p,q,r;

    p=B;
    init(p);
    p->num=1;

    q=B;
    init(q);
    q->num=2;

    r=B;
    init(r);
    r->num=3;

    int Occurtime=0,next_occur,sparetime=0;
    int test=1;
    Node *s;
    printf("**************************************************\n");
    while(test<=maxn)
    {
        srand(time(0));
        s=(Node *)malloc(sizeof(Node));
        s->next=NULL;
        s->customer=test;

        if(test==1)
        {
            s->Arrive=0;
            sparetime=rand()%5+5;
            s->wait=0;
            s->work=sparetime;
        }
        else
        {
            next_occur=rand()%3+1;
            Occurtime+=next_occur;
            s->Arrive=Occurtime;
            sparetime=rand()%3+5;
            s->work=sparetime;
        }
        test++;
        Handle(p,q,r,s);
        PrintAll(p,q,r);
        printf("**************************************************\n");
    }
    solve(p,q,r);
    PrintAll(p,q,r);
    int all=p->total+q->total+r->total;
    printf("总花费时间:%d\n",all);
    printf("总人数为:%d\n",maxn);
    printf("平均逗留时间为:%.2lf\n",(double)all/maxn);
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐