1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
| 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; } } } } } }
|