# 用C#表达式树优雅的计算24点

class Node
{
public Node Left { get; private set; }
public Node Right { get; private set; }
public Node(Node left, Node right)
{
this.Left = left;
this.Right = right;
}
}

static IEnumerable<Node> AllBinaryTrees(int size)
{
if (size == 0)
return new Node[] { null };
return from i in Enumerable.Range(0, size)
from left in AllBinaryTrees(i)
from right in AllBinaryTrees(size - 1 - i)
select new Node(left, right);
}

static Expression Build(Node node, List<double> numbers, List<Func<Expression, Expression, BinaryExpression>> operators)
{
var iNum = 0;
var iOprt = 0;

Func<Node, Expression> f = null;
f = n =>
{
Expression exp;
if (n == null)
exp = Expression.Constant(numbers[iNum++]);
else
{
var left = f(n.Left);
var right = f(n.Right);
exp = operators[iOprt++](left, right);
}
return exp;
};
return f(node);
}

static IEnumerable<List<T>> FullPermute<T>(List<T> elements)
{
if (elements.Count == 1)
return EnumerableOfOneElement(elements);

IEnumerable<List<T>> result = null;
foreach (T first in elements)
{
List<T> remaining = elements.ToArray().ToList();
remaining.Remove(first);
IEnumerable<List<T>> fullPermuteOfRemaining = FullPermute(remaining);

foreach (List<T> permute in fullPermuteOfRemaining)
{
var arr = new List<T> { first };
arr.AddRange(permute);

var seq = EnumerableOfOneElement(arr);
if (result == null)
result = seq;
else
result = result.Union(seq);
}
}
return result;
}

static IEnumerable<T> EnumerableOfOneElement<T>(T element)
{
yield return element;
}

1, 2, 3, 4
1, 2, 4, 3
1, 3, 2, 4
1, 3, 4, 2
1, 4, 2, 3
1, 4, 3, 2
2, 1, 3, 4
2, 1, 4, 3
2, 3, 1, 4
2, 3, 4, 1
2, 4, 1, 3
2, 4, 3, 1
3, 1, 2, 4
3, 1, 4, 2
3, 2, 1, 4
3, 2, 4, 1
3, 4, 1, 2
3, 4, 2, 1
4, 1, 2, 3
4, 1, 3, 2
4, 2, 1, 3
4, 2, 3, 1
4, 3, 1, 2
4, 3, 2, 1

static IEnumerable<IEnumerable<Func<Expression, Expression, BinaryExpression>>> OperatorPermute(List<Func<Expression, Expression, BinaryExpression>> operators)
{
return from operator1 in operators
from operator2 in operators
from operator3 in operators
select new[] { operator1, operator2, operator3 };
}

static void Main(string[] args)
{
List<double> numbers = new List<double> { 1, 2, 3, 4 };
var operators = new List<Func<Expression, Expression, BinaryExpression>> {
Expression.Add,Expression.Subtract,Expression.Multiply,Expression.Divide
};

foreach (var operatorCombination in OperatorPermute(operators))
{
foreach (Node node in AllBinaryTrees(3))
{
foreach (List<double> permuteOfNumbers in FullPermute(numbers))
{
Expression expression = Build(node, permuteOfNumbers, operatorCombination.ToList());
Func<double> compiled = Expression.Lambda<Func<double>>(expression).Compile();
try
{
var value = compiled();
if (Math.Abs(value - 24) < 0.01)
Console.WriteLine("{0} = {1}", expression, value);
}
catch (DivideByZeroException) { }
}
}
}
Console.Read();
}

(4 * (1 + (2 + 3))) = 24
(4 * (1 + (3 + 2))) = 24
(4 * (2 + (1 + 3))) = 24
(4 * (2 + (3 + 1))) = 24
(4 * (3 + (1 + 2))) = 24
(4 * (3 + (2 + 1))) = 24
(4 * ((1 + 2) + 3)) = 24
(4 * ((1 + 3) + 2)) = 24
(4 * ((2 + 1) + 3)) = 24
(4 * ((2 + 3) + 1)) = 24
(4 * ((3 + 1) + 2)) = 24
(4 * ((3 + 2) + 1)) = 24
((1 + 3) * (2 + 4)) = 24
((1 + 3) * (4 + 2)) = 24
((2 + 4) * (1 + 3)) = 24
((2 + 4) * (3 + 1)) = 24
((3 + 1) * (2 + 4)) = 24
((3 + 1) * (4 + 2)) = 24
((4 + 2) * (1 + 3)) = 24
((4 + 2) * (3 + 1)) = 24
((1 + (2 + 3)) * 4) = 24
((1 + (3 + 2)) * 4) = 24
((2 + (1 + 3)) * 4) = 24
((2 + (3 + 1)) * 4) = 24
((3 + (1 + 2)) * 4) = 24
((3 + (2 + 1)) * 4) = 24
(((1 + 2) + 3) * 4) = 24
(((1 + 3) + 2) * 4) = 24
(((2 + 1) + 3) * 4) = 24
(((2 + 3) + 1) * 4) = 24
(((3 + 1) + 2) * 4) = 24
(((3 + 2) + 1) * 4) = 24
(1 * (2 * (3 * 4))) = 24
(1 * (2 * (4 * 3))) = 24
(1 * (3 * (2 * 4))) = 24
(1 * (3 * (4 * 2))) = 24
(1 * (4 * (2 * 3))) = 24
(1 * (4 * (3 * 2))) = 24
(2 * (1 * (3 * 4))) = 24
(2 * (1 * (4 * 3))) = 24
(2 * (3 * (1 * 4))) = 24
(2 * (3 * (4 * 1))) = 24
(2 * (4 * (1 * 3))) = 24
(2 * (4 * (3 * 1))) = 24
(3 * (1 * (2 * 4))) = 24
(3 * (1 * (4 * 2))) = 24
(3 * (2 * (1 * 4))) = 24
(3 * (2 * (4 * 1))) = 24
(3 * (4 * (1 * 2))) = 24
(3 * (4 * (2 * 1))) = 24
(4 * (1 * (2 * 3))) = 24
(4 * (1 * (3 * 2))) = 24
(4 * (2 * (1 * 3))) = 24
(4 * (2 * (3 * 1))) = 24
(4 * (3 * (1 * 2))) = 24
(4 * (3 * (2 * 1))) = 24
(1 * ((2 * 3) * 4)) = 24
(1 * ((2 * 4) * 3)) = 24
(1 * ((3 * 2) * 4)) = 24
(1 * ((3 * 4) * 2)) = 24
(1 * ((4 * 2) * 3)) = 24
(1 * ((4 * 3) * 2)) = 24
(2 * ((1 * 3) * 4)) = 24
(2 * ((1 * 4) * 3)) = 24
(2 * ((3 * 1) * 4)) = 24
(2 * ((3 * 4) * 1)) = 24
(2 * ((4 * 1) * 3)) = 24
(2 * ((4 * 3) * 1)) = 24
(3 * ((1 * 2) * 4)) = 24
(3 * ((1 * 4) * 2)) = 24
(3 * ((2 * 1) * 4)) = 24
(3 * ((2 * 4) * 1)) = 24
(3 * ((4 * 1) * 2)) = 24
(3 * ((4 * 2) * 1)) = 24
(4 * ((1 * 2) * 3)) = 24
(4 * ((1 * 3) * 2)) = 24
(4 * ((2 * 1) * 3)) = 24
(4 * ((2 * 3) * 1)) = 24
(4 * ((3 * 1) * 2)) = 24
(4 * ((3 * 2) * 1)) = 24
((1 * 2) * (3 * 4)) = 24
((1 * 2) * (4 * 3)) = 24
((1 * 3) * (2 * 4)) = 24
((1 * 3) * (4 * 2)) = 24
((1 * 4) * (2 * 3)) = 24
((1 * 4) * (3 * 2)) = 24
((2 * 1) * (3 * 4)) = 24
((2 * 1) * (4 * 3)) = 24
((2 * 3) * (1 * 4)) = 24
((2 * 3) * (4 * 1)) = 24
((2 * 4) * (1 * 3)) = 24
((2 * 4) * (3 * 1)) = 24
((3 * 1) * (2 * 4)) = 24
((3 * 1) * (4 * 2)) = 24
((3 * 2) * (1 * 4)) = 24
((3 * 2) * (4 * 1)) = 24
((3 * 4) * (1 * 2)) = 24
((3 * 4) * (2 * 1)) = 24
((4 * 1) * (2 * 3)) = 24
((4 * 1) * (3 * 2)) = 24
((4 * 2) * (1 * 3)) = 24
((4 * 2) * (3 * 1)) = 24
((4 * 3) * (1 * 2)) = 24
((4 * 3) * (2 * 1)) = 24
((1 * (2 * 3)) * 4) = 24
((1 * (2 * 4)) * 3) = 24
((1 * (3 * 2)) * 4) = 24
((1 * (3 * 4)) * 2) = 24
((1 * (4 * 2)) * 3) = 24
((1 * (4 * 3)) * 2) = 24
((2 * (1 * 3)) * 4) = 24
((2 * (1 * 4)) * 3) = 24
((2 * (3 * 1)) * 4) = 24
((2 * (3 * 4)) * 1) = 24
((2 * (4 * 1)) * 3) = 24
((2 * (4 * 3)) * 1) = 24
((3 * (1 * 2)) * 4) = 24
((3 * (1 * 4)) * 2) = 24
((3 * (2 * 1)) * 4) = 24
((3 * (2 * 4)) * 1) = 24
((3 * (4 * 1)) * 2) = 24
((3 * (4 * 2)) * 1) = 24
((4 * (1 * 2)) * 3) = 24
((4 * (1 * 3)) * 2) = 24
((4 * (2 * 1)) * 3) = 24
((4 * (2 * 3)) * 1) = 24
((4 * (3 * 1)) * 2) = 24
((4 * (3 * 2)) * 1) = 24
(((1 * 2) * 3) * 4) = 24
(((1 * 2) * 4) * 3) = 24
(((1 * 3) * 2) * 4) = 24
(((1 * 3) * 4) * 2) = 24
(((1 * 4) * 2) * 3) = 24
(((1 * 4) * 3) * 2) = 24
(((2 * 1) * 3) * 4) = 24
(((2 * 1) * 4) * 3) = 24
(((2 * 3) * 1) * 4) = 24
(((2 * 3) * 4) * 1) = 24
(((2 * 4) * 1) * 3) = 24
(((2 * 4) * 3) * 1) = 24
(((3 * 1) * 2) * 4) = 24
(((3 * 1) * 4) * 2) = 24
(((3 * 2) * 1) * 4) = 24
(((3 * 2) * 4) * 1) = 24
(((3 * 4) * 1) * 2) = 24
(((3 * 4) * 2) * 1) = 24
(((4 * 1) * 2) * 3) = 24
(((4 * 1) * 3) * 2) = 24
(((4 * 2) * 1) * 3) = 24
(((4 * 2) * 3) * 1) = 24
(((4 * 3) * 1) * 2) = 24
(((4 * 3) * 2) * 1) = 24
((2 * (3 * 4)) / 1) = 24
((2 * (4 * 3)) / 1) = 24
((3 * (2 * 4)) / 1) = 24
((3 * (4 * 2)) / 1) = 24
((4 * (2 * 3)) / 1) = 24
((4 * (3 * 2)) / 1) = 24
(((2 * 3) * 4) / 1) = 24
(((2 * 4) * 3) / 1) = 24
(((3 * 2) * 4) / 1) = 24
(((3 * 4) * 2) / 1) = 24
(((4 * 2) * 3) / 1) = 24
(((4 * 3) * 2) / 1) = 24
(2 * ((3 * 4) / 1)) = 24
(2 * ((4 * 3) / 1)) = 24
(3 * ((2 * 4) / 1)) = 24
(3 * ((4 * 2) / 1)) = 24
(4 * ((2 * 3) / 1)) = 24
(4 * ((3 * 2) / 1)) = 24
((2 * 3) * (4 / 1)) = 24
((2 * 4) * (3 / 1)) = 24
((3 * 2) * (4 / 1)) = 24
((3 * 4) * (2 / 1)) = 24
((4 * 2) * (3 / 1)) = 24
((4 * 3) * (2 / 1)) = 24
(((2 * 3) / 1) * 4) = 24
(((2 * 4) / 1) * 3) = 24
(((3 * 2) / 1) * 4) = 24
(((3 * 4) / 1) * 2) = 24
(((4 * 2) / 1) * 3) = 24
(((4 * 3) / 1) * 2) = 24
(2 / (1 / (3 * 4))) = 24
(2 / (1 / (4 * 3))) = 24
(3 / (1 / (2 * 4))) = 24
(3 / (1 / (4 * 2))) = 24
(4 / (1 / (2 * 3))) = 24
(4 / (1 / (3 * 2))) = 24
((2 * 3) / (1 / 4)) = 24
((2 * 4) / (1 / 3)) = 24
((3 * 2) / (1 / 4)) = 24
((3 * 4) / (1 / 2)) = 24
((4 * 2) / (1 / 3)) = 24
((4 * 3) / (1 / 2)) = 24
(2 * (3 * (4 / 1))) = 24
(2 * (4 * (3 / 1))) = 24
(3 * (2 * (4 / 1))) = 24
(3 * (4 * (2 / 1))) = 24
(4 * (2 * (3 / 1))) = 24
(4 * (3 * (2 / 1))) = 24
(2 * ((3 / 1) * 4)) = 24
(2 * ((4 / 1) * 3)) = 24
(3 * ((2 / 1) * 4)) = 24
(3 * ((4 / 1) * 2)) = 24
(4 * ((2 / 1) * 3)) = 24
(4 * ((3 / 1) * 2)) = 24
((2 / 1) * (3 * 4)) = 24
((2 / 1) * (4 * 3)) = 24
((3 / 1) * (2 * 4)) = 24
((3 / 1) * (4 * 2)) = 24
((4 / 1) * (2 * 3)) = 24
((4 / 1) * (3 * 2)) = 24
((2 * (3 / 1)) * 4) = 24
((2 * (4 / 1)) * 3) = 24
((3 * (2 / 1)) * 4) = 24
((3 * (4 / 1)) * 2) = 24
((4 * (2 / 1)) * 3) = 24
((4 * (3 / 1)) * 2) = 24
(((2 / 1) * 3) * 4) = 24
(((2 / 1) * 4) * 3) = 24
(((3 / 1) * 2) * 4) = 24
(((3 / 1) * 4) * 2) = 24
(((4 / 1) * 2) * 3) = 24
(((4 / 1) * 3) * 2) = 24
(2 * (3 / (1 / 4))) = 24
(2 * (4 / (1 / 3))) = 24
(3 * (2 / (1 / 4))) = 24
(3 * (4 / (1 / 2))) = 24
(4 * (2 / (1 / 3))) = 24
(4 * (3 / (1 / 2))) = 24
((2 / (1 / 3)) * 4) = 24
((2 / (1 / 4)) * 3) = 24
((3 / (1 / 2)) * 4) = 24
((3 / (1 / 4)) * 2) = 24
((4 / (1 / 2)) * 3) = 24
((4 / (1 / 3)) * 2) = 24
(2 / ((1 / 3) / 4)) = 24
(2 / ((1 / 4) / 3)) = 24
(3 / ((1 / 2) / 4)) = 24
(3 / ((1 / 4) / 2)) = 24
(4 / ((1 / 2) / 3)) = 24
(4 / ((1 / 3) / 2)) = 24

posted @ 2014-10-15 22:34  Create Chen  阅读(4676)  评论(2编辑  收藏