数据结构上机题目--离散时间模拟(银行等待问题)
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; }
相关文章推荐
- 队列应用银行排队问题模拟:计算客户的平均停留时间和等待时间以及每个客户的时间信息,两种方法实现
- 数据结构上机题目4--后缀表达式求值
- 数据结构作业 迷宫问题 模拟 之基础的BFS DFS
- 数据结构之---C语言实现银行模拟(离散化)
- C++队列模拟银行排队叫号(加州大学伯克利分校计算机专业数据结构与算法作业)
- 数据结构——离散事件模拟
- jzoj5249 【NOIP2017提高A组模拟8.10】文本编辑器 (序列修改类问题,数据结构)
- 数据结构上机时间第三周项目4(1)—顺序表应用
- 离散事件模拟--银行排队时间模拟
- 数据结构上机题目--搜索
- 今天遇到一个问题:在程序中加入的定时触发器,当时出现这样一个问题,触发器定时2秒,程序从后台查询数据经过复杂处理后时间超过了2秒,我查阅好些网络上说有可能触发器等待程序执行完毕后,在触发下一次,也有说
- 银行排队问题之单队列多窗口加VIP服务--PTA数据与结构
- PTA 数据结构题目(1):最大子列和问题(分而治之、在线处理算法)
- 21、数据结构笔记之十九列队实现离散事件模拟
- 数据结构课程设计----银行模拟系统
- PTA 数据结构与算法题目集 5-1 最大子列和问题
- 22、数据结构笔记之十九列队实现离散事件模拟
- 数据结构课程设计——题目四电话模拟
- 【数据结构】3、模拟银行窗口排队叫号系统——C++
- 压缩、解压 解决 客户端查询大批量数据时等待时间过长的问题