数据结构:用队列模拟理发店的排队情况(C#)
来源:互联网 发布:unity 烘焙 知乎 编辑:程序博客网 时间:2024/05/03 23:16
题目内容:使用的排队现象,通过仿真手法评估其营业状况。
*基本要求:设某理发馆有N把理发椅,可同时为N位顾客进行理发。
*当顾客进门时,若有空椅,则可以立即坐下理发,否则需要依次排队等候。
*一旦有顾客理完发离去时,排在队头的顾客便可开始理发。
*若理发馆每天连续营业T小时,求一天内顾客在理发馆内的平均逗留时间
*顾客排队等候的队列平均长度
N和T在运行的时候输入
用C#写的,有注释,很混乱,请高人指教~~
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Collections;
- namespace 队列应用
- {
- class Seat
- {
- public bool IsFree;
- public Customer cus=null;
- public Seat(bool b)
- {
- IsFree = b;
- }
- }
- class Customer
- {
- public int cometime;
- public int timetogo;
- public int cost =30- new Random().Next(10);//理发需要20~30分钟
- public Customer() { }
- }
- class Program
- {
- static void Main(string[] args)
- {
- System.Console.WriteLine("每天营业多少小时?");
- int workinghours = int.Parse(Console.ReadLine());
- System.Console.WriteLine("有多少个椅子?");
- int seats = int.Parse(Console.ReadLine());
- Process(seats,workinghours);
- Console.ReadLine();
- }
- static void Process (int num,int time)
- {
- //------------准备变量------------------------
- Queue q = new Queue();//队列
- Seat[] S=new Seat[num];//所有的椅子
- for (int i = 0; i <num; i++)//初始化椅子
- {
- S[i] = new Seat(true);
- S[i].cus = null;
- }
- int somebodycome = 1;//第一个顾客来的时间
- //int count = 0;//顾客计数器
- List<Customer> cuslist = new List<Customer>();
- int st = num;//椅子数
- int Qlen = 0;//队列长度
- int Qchangetime = 0;//队列长度改变次数
- //------------准备变量--------------------------
- for (int t = 1; t <= time*60; t ++)//时间从第一分钟开始流逝,每分钟检查状态
- {
- //--------检查现在有没有人需要离开--------------
- CheckLeave(S, t);
- //--------检查现在有没有人需要离开--------------
- //-------检查排队的人是否可以找到座位---------
- CheckSeat(q, S, ref Qlen, ref Qchangetime, t);
- //-------检查排队的人是否可以找到座位---------
- //-------------如果来人了,有座位就坐下,没座位就排队-------------------
- if (t == somebodycome)
- {
- bool IsSitted = false;//当前刚来的顾客是否找到了座位
- Customer c = new Customer();
- c.cometime = t;
- cuslist.Add(c);
- foreach (Seat s in S)
- {
- if (s.IsFree == true)
- {
- s.IsFree = false;
- s.cus = c;
- s.cus.timetogo = t+s.cus.cost;
- IsSitted = true;
- break;
- }
- }
- if (IsSitted == false)
- {
- q.Enqueue(c);
- Qlen += q.Count;
- Qchangetime++;
- }
- else
- {
- IsSitted = false;
- }
- somebodycome +=10- new Random().Next(5);//下一个顾客来的时间,假设5~10分钟之内会有一个
- }
- //------如果来人了,有座位就坐下,没座位就排队---------
- }
- //---------加班----------
- bool Inseat = true;
- //bool InQ = true;
- bool KeepWorking = true;
- int curtime = time * 60 + 1;
- while (KeepWorking)
- {
- CheckLeave(S, curtime);
- CheckSeat(q, S, ref Qlen, ref Qchangetime, curtime);
- foreach (Seat s in S)
- {
- if (s.IsFree==false)
- {
- Inseat = true;
- break;
- }
- else
- {
- Inseat = false;
- }
- }
- KeepWorking = Inseat;
- curtime++;
- }
- //---------加班----------
- //--------------
- int no = 1;
- foreach (Customer c in cuslist)
- {
- Console.Write("第{0}个顾客 " ,no);
- Console.Write("来的时间:"+c.cometime);
- Console.WriteLine(" 走的时间:"+c.timetogo);
- no++;
- }//------------------
- //--------打印结果-------------
- int totalstaytime = 0;
- foreach (Customer c in cuslist)
- {
- int staytime = c.timetogo - c.cometime;
- totalstaytime += staytime;
- }
- int averragestay = totalstaytime / cuslist.Count;
- System.Console.WriteLine("平均逗留时间:"+averragestay);
- System.Console.WriteLine("顾客数量:" + cuslist.Count);
- if (Qchangetime != 0)
- {
- int averagelen = Qlen / Qchangetime;
- Console.WriteLine("队列平均长度:" + averagelen);
- }
- else
- {
- Console.WriteLine("椅子充足,不用排队");
- }
- Console.WriteLine("加班时间:"+(curtime-time*60)+"分钟");
- //--------打印结果------------
- }
- private static void CheckSeat(Queue q, Seat[] S, ref int Qlen, ref int Qchangetime, int t)
- {
- if (q.Count != 0)//如果有人排队
- {
- foreach (Seat s in S)
- {
- if (s.IsFree == true)
- {
- s.IsFree = false;
- s.cus = (Customer)q.Dequeue();
- Qlen += q.Count;
- Qchangetime++;
- s.cus.timetogo = t + s.cus.cost;//
- }
- }
- }
- }
- private static void CheckLeave(Seat[] S, int t)
- {
- foreach (Seat s in S)
- {
- if (s.cus != null)
- {
- if (s.cus.timetogo == t)
- {
- s.IsFree = true;
- }
- }
- }
- }
- }
- }
- 数据结构:用队列模拟理发店的排队情况(C#)
- 严蔚敏 数据结构C语言 银行排队队列 离散事件模拟
- c语言版数据结构(奇迹冬瓜)-队列实战(1)离散事件模拟(银行排队)
- 第七周项目五C/C++数据结构实践——排队看病模拟(队列)
- Java -- 队列(模拟银行的排队办理业务)
- C++队列模拟银行排队叫号(加州大学伯克利分校计算机专业数据结构与算法作业)
- 数据结构实践——排队看病模拟(队列)
- 数据结构第七周项目-排队看病模拟(队列)
- 队列Queue模拟排队问题
- 用队列模拟服务台前的排队现象问题_c/c++
- 第七周--数据结构--队列的实践之排队看病系统
- 第7周项目5-排队看病模拟(队列)
- 队列——排队买饭(数组模拟法)
- 3-9-模拟银行排队过程-栈和队列-第3章-《数据结构》课本源码-严蔚敏吴伟民版
- 数据结构---队列(C#)
- 【数据结构】顺序队列的实现(c++)
- 【数据结构】循环队列的实现(c++)
- 队列模拟(简单链表数据结构的处理)
- 在ASP.net中验证控件使用
- 彻底卸载Oracle
- 数据库事务特点
- ORA-12560: TNS: 协议适配器错误的问题
- VS2008 .net中 如何安装部署DB2 数据库应用
- 数据结构:用队列模拟理发店的排队情况(C#)
- mysql数据库备份与还原命令
- oracle 不能刷新快照解决方法
- 判断字符是不是数字
- 活用sudo
- C# winform 程序打包部署-带数据库安装部署打包
- 兼容timeout的Java执行外部命令的小程序
- 解决输入文件中(如txt)回车换行的方法
- Windows操作快捷键(she's question)