六.第2局为什么是11种可能
回过头来解决为什么对于一个确定的第1局,第2局有11种可能。
不妨假设第1局的选择是A选1号椅,B选2号椅,C选3号椅,D选4号椅。
第2局分为两大类情况:
如果B选了第5号椅
则只有2种可能:
A B C D .-D . A C B
A B C D .-C . D A B
如果B选了不是第5号椅,
则ACD都有可能选第5号椅,有3种可能。B有3种选的可能(1,3,4号椅),B一旦确定,A和C也只有一种可能
所以11 = 2 + 3 * 3
七.结论
由一道数学题牵引出多层循环嵌套,最终通过封装达到了我要的链式调用的效果,我是很满意的。这也是我第一次设计延迟计算,感觉强烈。如果新的场景需要用到延迟计算我想有了这次经验写起来会顺手许多。如果是需要多层for的算法题都可以比较方便的实现了。
你都看到这里了,为我点个赞吧,能说一下看法就更好了。
完整代码:
1 using System; 2 using System.Linq; 3 using System.Diagnostics; ConsoleApplication1 6 { 7 class Seat 8 { 9 static Seat data = new Seat(); Run() 11 { 12 //Seat2.Run(); (int a = 0; a < 4; a++) 15 { ; ); (int b = 0; b < 4; b++) 20 { 21 if (data.IsSelected(0, b)) 22 continue; ); 24 for (int c = 0; c < 4; c++) 25 { 26 if (data.IsSelected(0, c)) 27 continue; ); 29 for (int d = 0; d < 4; d++) 30 { 31 if (data.IsSelected(0, d)) 32 continue; ); 34 for (int a2 = 0; a2 < 5; a2++) 35 { 36 if (a2 == 1) 37 continue; ; )) ; ); 43 for (int b2 = 0; b2 < 5; b2++) 44 { 45 if (b2 == 1) 46 continue; 47 if (data.IsSelected(1, b2)) 48 continue; )) 50 continue; ); 52 for (int c2 = 0; c2 < 5; c2++) 53 { 54 if (c2 == 1) 55 continue; 56 if (data.IsSelected(1, c2)) 57 continue; )) 59 continue; ); 61 for (int d2 = 0; d2 < 5; d2++) 62 { 63 if (d2 == 1) 64 continue; 65 if (data.IsSelected(1, d2)) 66 continue; )) 68 continue; ); Console.WriteLine(, data.Count, data.Current); 73 74 data.UnSelected(1, d2); 75 } 76 data.UnSelected(1, c2); 77 } 78 data.UnSelected(1, b2); 79 } 80 data.UnSelected(1, a2); 81 } 82 data.UnSelected(0, d); 83 } 84 data.UnSelected(0, c); 85 } 86 data.UnSelected(0, b); 87 } } 90 } Run2() 92 { , , , , , , , , ) 103 ) 104 ) 105 ) 106 ) 107 ) 108 ) 109 ); 110 } Try(string name, Action action) 112 { 113 for (int i = 0; i < 4; i++) 114 { 115 if (data.IsSelected(0, i)) 116 continue; 117 data.Selected(0, i, name); 118 if (action == null) 119 { 120 Console.WriteLine(data.Current); 121 } { 124 action(); 125 } 126 data.UnSelected(0, i); 127 } 128 } Try2(string name, Action action) 131 { 132 for (int i = 0; i < 5; i++) 133 { 134 if (i == 1) 135 continue; 136 if (data.IsSelected(1, i)) 137 continue; 138 if (data.IsSelected(0, i, name)) 139 continue; 140 data.Selected(1, i, name); 141 if (action == null) 142 { 143 data.Count++; , data.Count, data.Current); 145 } { 148 action(); 149 } 150 data.UnSelected(1, i); 151 } 152 } 153 public Seat() 154 { ; ; 157 } [,] seats = new string[2, 5]; UnSelected(int game, int i) 160 { 161 Debug.Assert(game == 0 && i != 4 || game == 1 && i != 1); 162 Debug.Assert(seats[game, i] != null); 163 seats[game, i] = null; 164 } Selected(int game, int i, string name) 166 { 167 Debug.Assert(game == 0 && i != 4 || game == 1 && i != 1); 168 Debug.Assert(seats[game, i] == null); 169 seats[game, i] = name; 170 } IsSelected(int game, int a) 172 { ; 174 } IsSelected(int game, int a, string name) 176 { 177 return seats[game, a] == name; 178 } Current 180 { { .Format(, 184 seats[0, 0], seats[0, 1], seats[0, 2], seats[0, 3], seats[0, 4], 185 seats[1, 0], seats[1, 1], seats[1, 2], seats[1, 3], seats[1, 4]); 186 } 187 } Count { get; set; } 190 } Seat2 193 { Seat2 Parent { get; set; } 196 Seat2 Child { get; set; } 197 string Name { get; set; } 198 Action<Seat2> Method { get; set; } 199 public Seat2(string name, Seat2 parent, Action<Seat2> method) 200 { 201 this.Name = name; 202 this.Parent = parent; 203 if (parent != null) 204 parent.Child = this; 205 this.Method = method; 206 } 耦合的版本 Run() 211 { , null, me => me.Try()) , me => me.Try()) , me => me.Try()) , me => me.Try()) , me => me.Try2()) , me => me.Try2()) , me => me.Try2()) , me => me.Try2()) 220 .P().Start(); 221 } 222 public Seat2 T(string name, Action<Seat2> method) 223 { Seat2(name, this, method); 225 } 226 public Seat2 P() 227 { Seat2(, this, me => me.Print()); 229 } Start() 231 { 232 var head = this.Head; 233 head.Method(head); 234 } 235 public Seat2 Head 236 { { 239 if (null != this.Parent) .Parent.Head; ; 242 } 243 } Try() 245 { 246 for (int i = 0; i < 4; i++) 247 { 248 if (data.IsSelected(0, i)) 249 continue; 250 data.Selected(0, i, this.Name); 251 if (this.Child != null) 252 { 253 this.Child.Method(this.Child); 254 } 255 data.UnSelected(0, i); 256 } 257 } Try2() 259 { 260 for (int i = 0; i < 5; i++) 261 { 262 if (i == 1) 263 continue; 264 if (data.IsSelected(1, i)) 265 continue; 266 if (data.IsSelected(0, i, this.Name)) 267 continue; 268 data.Selected(1, i, this.Name); 269 if (this.Child != null) 270 { 271 this.Child.Method(this.Child); 272 } 273 data.UnSelected(1, i); 274 } 275 } Print() 277 { 278 data.Count++; , data.Count, data.Current); 280 } ToString() 283 { .Name.ToString(); 285 } 286 } ComputeLink<T> where T : ISeat3 289 { ComputeLink<T> Child { T Obj { ComputeLink(T obj, ComputeLink<T> parent, Action<T> method) 294 { 295 if (obj == null) ArgumentNullException(); 297 this.Obj = obj; 298 this.Obj.Method = x => method((T)x); 299 if (parent != null) 300 { 301 this.Parent = parent; 302 parent.Child = this; 303 parent.Obj.Child = this.Obj; 304 } 305 } ComputeLink<T> New(T obj, Action<T> method) 307 { ComputeLink<T>(obj, null, method); 309 } ComputeLink<T> Do(T obj, Action<T> method) 312 { ComputeLink<T>(obj, this, method); 314 } { { 319 if (null != this.Parent) .Parent.Head; ; 322 } 323 } Action() { 326 var head = this.Head; 327 head.Obj.Method(head.Obj); 328 } 329 } 330 interface ISeat3 331 { 332 ISeat3 Child { get; set; } 333 Action<ISeat3> Method { get; set; } 334 } 335 class Seat3 : ISeat3 336 { 337 static Seat data = new Seat(); 338 string Name { get; set; } 339 public Seat3(string name) 340 { 341 this.Name = name; 342 } 解耦的版本 Run() 347 { 348 var sql = ComputeLink<Seat3> ), m => m.Try()) ), m => m.Try()) ), m => m.Try()) ), m => m.Try()) ), m => m.Try2()) ), m => m.Try2()) ), m => m.Try2()) ), m => m.Try2()) 357 .Do(new Seat3(""), m => m.Print()); 358 sql.Action(); 359 } 360 public Action<ISeat3> Method { get; set; } 361 public ISeat3 Child { get; set; } Try() 363 { 364 for (int i = 0; i < 4; i++) 365 { 366 if (data.IsSelected(0, i)) 367 continue; 368 data.Selected(0, i, this.Name); 369 if (this.Child != null) 370 { 371 this.Child.Method(this.Child); 372 } 373 data.UnSelected(0, i); 374 } 375 } Try2() 377 { 378 for (int i = 0; i < 5; i++) 379 { 380 if (i == 1) 381 continue; 382 if (data.IsSelected(1, i)) 383 continue; 384 if (data.IsSelected(0, i, this.Name)) 385 continue; 386 data.Selected(1, i, this.Name); 387 if (this.Child != null) 388 { 389 this.Child.Method(this.Child); 390 } 391 data.UnSelected(1, i); 392 } 393 } Print() 395 { 396 data.Count++; , data.Count, data.Current); 398 } ToString() 401 { .Name.ToString(); 403 } 404 } 405 }
View Code