六.第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的算法题都可以比较方便的实现了。
你都看到这里了,为我点个赞吧,能说一下看法就更好了。
完整代码:
using System;
using System.Linq;
using System.Diagnostics;
namespace ConsoleApplication
{
class Seat
{
static Seat data = new Seat();
public static void Run()
{
//Seat.Run();
//return;
for (int a = ; a < ; a++)
{
if (data.IsSelected(, a)) //第局编号。如果已经被人坐了。
continue;
data.Selected(, a, "A"); //第局编号。A坐a椅。
for (int b = ; b < ; b++)
{
if (data.IsSelected(, b))
continue;
data.Selected(, b, "B");
for (int c = ; c < ; c++)
{
if (data.IsSelected(, c))
continue;
data.Selected(, c, "C");
for (int d = ; d < ; d++)
{
if (data.IsSelected(, d))
continue;
data.Selected(, d, "D");
for (int a = ; a < ; a++)
{
if (a == )
continue;
if (data.IsSelected(, a)) //第局编号
continue;
if (data.IsSelected(, a, "A")) //如果第局A坐了a椅
continue;
data.Selected(, a, "A");
for (int b = ; b < ; b++)
{
if (b == )
continue;
if (data.IsSelected(, b))
continue;
if (data.IsSelected(, b, "B"))
continue;
data.Selected(, b, "B");
for (int c = ; c < ; c++)
{
if (c == )
continue;
if (data.IsSelected(, c))
continue;
if (data.IsSelected(, c, "C"))
continue;
data.Selected(, c, "C");
for (int d = ; d < ; d++)
{
if (d == )
continue;
if (data.IsSelected(, d))
continue;
if (data.IsSelected(, d, "D"))
continue;
data.Selected(, d, "D");
data.Count++; //可能的情况数加
Console.WriteLine("{,} {}", data.Count, data.Current);
data.UnSelected(, d);
}
data.UnSelected(, c);
}
data.UnSelected(, b);
}
data.UnSelected(, a);
}
data.UnSelected(, d);
}
data.UnSelected(, c);
}
data.UnSelected(, b);
}
data.UnSelected(, a); //A起身(释放坐椅)
}
}
public static void Run()
{
Try("A",
() => Try("B",
() => Try("C",
() => Try("D",
() => Try("A",
() => Try("B",
() => Try("C",
() => Try("D",
null
)
)
)
)
)
)
)
);
}
public static void Try(string name, Action action)
{
for (int i = ; i < ; i++)
{
if (data.IsSelected(, i))
continue;
data.Selected(, i, name);
if (action == null)
{
Console.WriteLine(data.Current);
}
else
{
action();
}
data.UnSelected(, i);
}
}
public static void Try(string name, Action action)
{
for (int i = ; i < ; i++)
{
if (i == )
continue;
if (data.IsSelected(, i))
continue;
if (data.IsSelected(, i, name))
continue;
data.Selected(, i, name);
if (action == null)
{
data.Count++;
Console.WriteLine("{,} {}", data.Count, data.Current);
}
else
{
action();
}
data.UnSelected(, i);
}
}
public Seat()
{
seats[, ] = ".";
seats[, ] = ".";
}
private string[,] seats = new string[, ];
public void UnSelected(int game, int i)
{
Debug.Assert(game == && i != || game == && i != );
Debug.Assert(seats[game, i] != null);
seats[game, i] = null;
}
public void Selected(int game, int i, string name)
{
Debug.Assert(game == && i != || game == && i != );
Debug.Assert(seats[game, i] == null);
seats[game, i] = name;
}
public bool IsSelected(int game, int a)
{
return seats[game, a] != null && seats[game, a] != ".";
}
public bool IsSelected(int game, int a, string name)
{
return seats[game, a] == name;
}
public string Current
{
get
{
return string.Format("{} {} {} {} {}-{} {} {} {} {}",
seats[, ], seats[, ], seats[, ], seats[, ], seats[, ],
seats[, ], seats[, ], seats[, ], seats[, ], seats[, ]);
}
}
public int Count { get; set; }
}
class Seat
{
static Seat data = new Seat(); //借用Seat保存法的数据
Seat Parent { get; set; }
Seat Child { get; set; }
string Name { get; set; }
Action<Seat> Method { get; set; }
public Seat(string name, Seat parent, Action<Seat> method)
{
this.Name = name;
this.Parent = parent;
if (parent != null)
parent.Child = this;
this.Method = method;
}
/// <summary>
/// 耦合的版本
/// </summary>
public static void Run()
{
new Seat("A", null, me => me.Try())
.T("B", me => me.Try())
.T("C", me => me.Try())
.T("D", me => me.Try())
.T("A", me => me.Try())
.T("B", me => me.Try())
.T("C", me => me.Try())
.T("D", me => me.Try())
.P().Start();
}
public Seat T(string name, Action<Seat> method)
{
return new Seat(name, this, method);
}
public Seat P()
{
return new Seat("Print", this, me => me.Print());
}
public void Start()
{
var head = this.Head;
head.Method(head);
}
public Seat Head
{
get
{
if (null != this.Parent)
return this.Parent.Head;
return this;
}
}
public void Try()
{
for (int i = ; i < ; i++)
{
if (data.IsSelected(, i))
continue;
data.Selected(, i, this.Name);
if (this.Child != null)
{
this.Child.Method(this.Child);
}
data.UnSelected(, i);
}
}
public void Try()
{
for (int i = ; i < ; i++)
{
if (i == )
continue;
if (data.IsSelected(, i))
continue;
if (data.IsSelected(, i, this.Name))
continue;
data.Selected(, i, this.Name);
if (this.Child != null)
{
this.Child.Method(this.Child);
}
data.UnSelected(, i);
}
}
public void Print()
{
data.Count++;
Console.WriteLine("{,} {}", data.Count, data.Current);
}
public override string ToString()
{
return this.Name.ToString();
}
}
class ComputeLink<T> where T : ISeat
{
ComputeLink<T> Parent { get; set; } //父节点,即上一级节点
ComputeLink<T> Child { get; set; } //子节点,即下一级节点
T Obj { get; set; } //当前节点对应的算法对象,可以看作业务对象
public ComputeLink(T obj, ComputeLink<T> parent, Action<T> method)
{
if (obj == null)
throw new ArgumentNullException("obj");
this.Obj = obj;
this.Obj.Method = x => method((T)x);
if (parent != null)
{
this.Parent = parent;
parent.Child = this;
parent.Obj.Child = this.Obj;
}
}
public static ComputeLink<T> New(T obj, Action<T> method)
{
return new ComputeLink<T>(obj, null, method);
}
public ComputeLink<T> Do(T obj, Action<T> method)
{
return new ComputeLink<T>(obj, this, method);
}
public ComputeLink<T> Head //链表的头
{
get
{
if (null != this.Parent)
return this.Parent.Head;
return this;
}
}
public void Action() //启动(延迟的)整个计算
{
var head = this.Head;
head.Obj.Method(head.Obj);
}
}
interface ISeat
{
ISeat Child { get; set; }
Action<ISeat> Method { get; set; }
}
class Seat : ISeat
{
static Seat data = new Seat();
string Name { get; set; }
public Seat(string name)
{
this.Name = name;
}
/// <summary>
/// 解耦的版本
/// </summary>
public static void Run()
{
var sql = ComputeLink<Seat>
.New(new Seat("A"), m => m.Try())
.Do(new Seat("B"), m => m.Try())
.Do(new Seat("C"), m => m.Try())
.Do(new Seat("D"), m => m.Try())
.Do(new Seat("A"), m => m.Try())
.Do(new Seat("B"), m => m.Try())
.Do(new Seat("C"), m => m.Try())
.Do(new Seat("D"), m => m.Try())
.Do(new Seat(""), m => m.Print());
sql.Action();
}
public Action<ISeat> Method { get; set; }
public ISeat Child { get; set; }
public void Try()
{
for (int i = ; i < ; i++)
{
if (data.IsSelected(, i))
continue;
data.Selected(, i, this.Name);
if (this.Child != null)
{
this.Child.Method(this.Child);
}
data.UnSelected(, i);
}
}
public void Try()
{
for (int i = ; i < ; i++)
{
if (i == )
continue;
if (data.IsSelected(, i))
continue;
if (data.IsSelected(, i, this.Name))
continue;
data.Selected(, i, this.Name);
if (this.Child != null)
{
this.Child.Method(this.Child);
}
data.UnSelected(, i);
}
}
public void Print()
{
data.Count++;
Console.WriteLine("{,} {}", data.Count, data.Current);
}
public override string ToString()
{
return this.Name.ToString();
}
}
}










