思路:一共4个数字,共需要3个运算符,可以构造一个二叉树,没有子节点的节点的为值,有叶子节点的为运算符

例如数字{1, 2, 3, 4},其中一种解的二叉树形式如下所示:

因此可以遍历所有二叉树可能的形式,4个数的全排列,从4种运算符中挑选3种运算符(运算符可以重复)

核心步骤1:需要遍历所有二叉树的可能,参考Eric Lippert的方法

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 == )
return new Node[] { null };
return from i in Enumerable.Range(, size)
from left in AllBinaryTrees(i)
from right in AllBinaryTrees(size - - i)
select new Node(left, right);
}

核心步骤2:对于任意一个二叉树,构造表达式树

static Expression Build(Node node, List<double> numbers, List<Func<Expression, Expression, BinaryExpression>> operators)
{
var iNum = ;
var iOprt = ; 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);
}

核心步骤3:遍历4个数字的全排列,全排列参考这里

static IEnumerable<List<T>> FullPermute<T>(List<T> elements)
{
if (elements.Count == )
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},它的全排列如下:

, , ,
, , ,
, , ,
, , ,
, , ,
, , ,
, , ,
, , ,
, , ,
, , ,
, , ,
, , ,
, , ,
, , ,
, , ,
, , ,
, , ,
, , ,
, , ,
, , ,
, , ,
, , ,
, , ,
, , ,

核心步骤4:从4种运算符中挑选3个运算符

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 };
}

最后是Main函数:

static void Main(string[] args)
{
List<double> numbers = new List<double> { , , , };
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())
{
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 - ) < 0.01)
Console.WriteLine("{0} = {1}", expression, value);
}
catch (DivideByZeroException) { }
}
}
}
Console.Read();
}

计算结果:

( * ( + ( + ))) =
( * ( + ( + ))) =
( * ( + ( + ))) =
( * ( + ( + ))) =
( * ( + ( + ))) =
( * ( + ( + ))) =
( * (( + ) + )) =
( * (( + ) + )) =
( * (( + ) + )) =
( * (( + ) + )) =
( * (( + ) + )) =
( * (( + ) + )) =
(( + ) * ( + )) =
(( + ) * ( + )) =
(( + ) * ( + )) =
(( + ) * ( + )) =
(( + ) * ( + )) =
(( + ) * ( + )) =
(( + ) * ( + )) =
(( + ) * ( + )) =
(( + ( + )) * ) =
(( + ( + )) * ) =
(( + ( + )) * ) =
(( + ( + )) * ) =
(( + ( + )) * ) =
(( + ( + )) * ) =
((( + ) + ) * ) =
((( + ) + ) * ) =
((( + ) + ) * ) =
((( + ) + ) * ) =
((( + ) + ) * ) =
((( + ) + ) * ) =
( * ( * ( * ))) =
( * ( * ( * ))) =
( * ( * ( * ))) =
( * ( * ( * ))) =
( * ( * ( * ))) =
( * ( * ( * ))) =
( * ( * ( * ))) =
( * ( * ( * ))) =
( * ( * ( * ))) =
( * ( * ( * ))) =
( * ( * ( * ))) =
( * ( * ( * ))) =
( * ( * ( * ))) =
( * ( * ( * ))) =
( * ( * ( * ))) =
( * ( * ( * ))) =
( * ( * ( * ))) =
( * ( * ( * ))) =
( * ( * ( * ))) =
( * ( * ( * ))) =
( * ( * ( * ))) =
( * ( * ( * ))) =
( * ( * ( * ))) =
( * ( * ( * ))) =
( * (( * ) * )) =
( * (( * ) * )) =
( * (( * ) * )) =
( * (( * ) * )) =
( * (( * ) * )) =
( * (( * ) * )) =
( * (( * ) * )) =
( * (( * ) * )) =
( * (( * ) * )) =
( * (( * ) * )) =
( * (( * ) * )) =
( * (( * ) * )) =
( * (( * ) * )) =
( * (( * ) * )) =
( * (( * ) * )) =
( * (( * ) * )) =
( * (( * ) * )) =
( * (( * ) * )) =
( * (( * ) * )) =
( * (( * ) * )) =
( * (( * ) * )) =
( * (( * ) * )) =
( * (( * ) * )) =
( * (( * ) * )) =
(( * ) * ( * )) =
(( * ) * ( * )) =
(( * ) * ( * )) =
(( * ) * ( * )) =
(( * ) * ( * )) =
(( * ) * ( * )) =
(( * ) * ( * )) =
(( * ) * ( * )) =
(( * ) * ( * )) =
(( * ) * ( * )) =
(( * ) * ( * )) =
(( * ) * ( * )) =
(( * ) * ( * )) =
(( * ) * ( * )) =
(( * ) * ( * )) =
(( * ) * ( * )) =
(( * ) * ( * )) =
(( * ) * ( * )) =
(( * ) * ( * )) =
(( * ) * ( * )) =
(( * ) * ( * )) =
(( * ) * ( * )) =
(( * ) * ( * )) =
(( * ) * ( * )) =
(( * ( * )) * ) =
(( * ( * )) * ) =
(( * ( * )) * ) =
(( * ( * )) * ) =
(( * ( * )) * ) =
(( * ( * )) * ) =
(( * ( * )) * ) =
(( * ( * )) * ) =
(( * ( * )) * ) =
(( * ( * )) * ) =
(( * ( * )) * ) =
(( * ( * )) * ) =
(( * ( * )) * ) =
(( * ( * )) * ) =
(( * ( * )) * ) =
(( * ( * )) * ) =
(( * ( * )) * ) =
(( * ( * )) * ) =
(( * ( * )) * ) =
(( * ( * )) * ) =
(( * ( * )) * ) =
(( * ( * )) * ) =
(( * ( * )) * ) =
(( * ( * )) * ) =
((( * ) * ) * ) =
((( * ) * ) * ) =
((( * ) * ) * ) =
((( * ) * ) * ) =
((( * ) * ) * ) =
((( * ) * ) * ) =
((( * ) * ) * ) =
((( * ) * ) * ) =
((( * ) * ) * ) =
((( * ) * ) * ) =
((( * ) * ) * ) =
((( * ) * ) * ) =
((( * ) * ) * ) =
((( * ) * ) * ) =
((( * ) * ) * ) =
((( * ) * ) * ) =
((( * ) * ) * ) =
((( * ) * ) * ) =
((( * ) * ) * ) =
((( * ) * ) * ) =
((( * ) * ) * ) =
((( * ) * ) * ) =
((( * ) * ) * ) =
((( * ) * ) * ) =
(( * ( * )) / ) =
(( * ( * )) / ) =
(( * ( * )) / ) =
(( * ( * )) / ) =
(( * ( * )) / ) =
(( * ( * )) / ) =
((( * ) * ) / ) =
((( * ) * ) / ) =
((( * ) * ) / ) =
((( * ) * ) / ) =
((( * ) * ) / ) =
((( * ) * ) / ) =
( * (( * ) / )) =
( * (( * ) / )) =
( * (( * ) / )) =
( * (( * ) / )) =
( * (( * ) / )) =
( * (( * ) / )) =
(( * ) * ( / )) =
(( * ) * ( / )) =
(( * ) * ( / )) =
(( * ) * ( / )) =
(( * ) * ( / )) =
(( * ) * ( / )) =
((( * ) / ) * ) =
((( * ) / ) * ) =
((( * ) / ) * ) =
((( * ) / ) * ) =
((( * ) / ) * ) =
((( * ) / ) * ) =
( / ( / ( * ))) =
( / ( / ( * ))) =
( / ( / ( * ))) =
( / ( / ( * ))) =
( / ( / ( * ))) =
( / ( / ( * ))) =
(( * ) / ( / )) =
(( * ) / ( / )) =
(( * ) / ( / )) =
(( * ) / ( / )) =
(( * ) / ( / )) =
(( * ) / ( / )) =
( * ( * ( / ))) =
( * ( * ( / ))) =
( * ( * ( / ))) =
( * ( * ( / ))) =
( * ( * ( / ))) =
( * ( * ( / ))) =
( * (( / ) * )) =
( * (( / ) * )) =
( * (( / ) * )) =
( * (( / ) * )) =
( * (( / ) * )) =
( * (( / ) * )) =
(( / ) * ( * )) =
(( / ) * ( * )) =
(( / ) * ( * )) =
(( / ) * ( * )) =
(( / ) * ( * )) =
(( / ) * ( * )) =
(( * ( / )) * ) =
(( * ( / )) * ) =
(( * ( / )) * ) =
(( * ( / )) * ) =
(( * ( / )) * ) =
(( * ( / )) * ) =
((( / ) * ) * ) =
((( / ) * ) * ) =
((( / ) * ) * ) =
((( / ) * ) * ) =
((( / ) * ) * ) =
((( / ) * ) * ) =
( * ( / ( / ))) =
( * ( / ( / ))) =
( * ( / ( / ))) =
( * ( / ( / ))) =
( * ( / ( / ))) =
( * ( / ( / ))) =
(( / ( / )) * ) =
(( / ( / )) * ) =
(( / ( / )) * ) =
(( / ( / )) * ) =
(( / ( / )) * ) =
(( / ( / )) * ) =
( / (( / ) / )) =
( / (( / ) / )) =
( / (( / ) / )) =
( / (( / ) / )) =
( / (( / ) / )) =
( / (( / ) / )) =

对于一些平时口算相对稍难的一些组合也是毫无压力,例如{1, 5, 5, 5}, {3, 3, 7, 7}, {3, 3, 8, 8},有兴趣的看官口算试试 :)

用C#表达式树优雅的计算24点的更多相关文章

  1. C#学习笔记(九):LINQ和表达式树

    LINQ LINQ:语言集成查询(Language Integrated Query)是一组用于c#和Visual Basic语言的扩展.它允许编写C#或者Visual Basic代码以查询数据库相同 ...

  2. 程序猿修仙之路--数据结构之你是否真的懂数组? c#socket TCP同步网络通信 用lambda表达式树替代反射 ASP.NET MVC如何做一个简单的非法登录拦截

    程序猿修仙之路--数据结构之你是否真的懂数组?   数据结构 但凡IT江湖侠士,算法与数据结构为必修之课.早有前辈已经明确指出:程序=算法+数据结构  .要想在之后的江湖历练中通关,数据结构必不可少. ...

  3. C#3.0新特性:隐式类型、扩展方法、自动实现属性,对象/集合初始值设定、匿名类型、Lambda,Linq,表达式树、可选参数与命名参数

    一.隐式类型var 从 Visual C# 3.0 开始,在方法范围中声明的变量可以具有隐式类型var.隐式类型可以替代任何类型,编译器自动推断类型. 1.var类型的局部变量必须赋予初始值,包括匿名 ...

  4. c# 表达式树(一)

    前言 打算整理c# 代码简化史系列,所以相关的整理一下,简单的引出一下概念. 什么是表达式树呢? 表达式树以树形数据结构表示代码,其中每一个节点都是一种表达式,比如方法调用和 x < y 这样的 ...

  5. C# 委托、事件、表达式树理解

    1.什么是委托? 委托是一种动态调用方法的类型,属于引用型. 委托是对方法的抽象和封装.委托对象实质上代表了方法的引用(即内存地址) 所有的异步都是委托   委托就是函数当入参   委托被各种语法糖遮 ...

  6. C# Lambda表达式详解,及Lambda表达式树的创建

    最近由于项目需要,刚刚学完了Action委托和Func<T>委托,发现学完了委托就必须学习lambda表达式,委托和Lambda表达式联合起来,才能充分的体现委托的便利.才能使代码更加简介 ...

  7. C#中的Lambda表达式和表达式树

    在C# 2.0中,通过方法组转换和匿名方法,使委托的实现得到了极大的简化.但是,匿名方法仍然有些臃肿,而且当代码中充满了匿名方法的时候,可读性可能就会受到影响.C# 3.0中出现的Lambda表达式在 ...

  8. LinQ实战学习笔记(三) 序列,查询操作符,查询表达式,表达式树

    序列 延迟查询执行 查询操作符 查询表达式 表达式树 (一) 序列 先上一段代码, 这段代码使用扩展方法实现下面的要求: 取进程列表,进行过滤(取大于10M的进程) 列表进行排序(按内存占用) 只保留 ...

  9. C#在泛型类中,通过表达式树构造lambda表达式

    场景 最近对爬虫的数据库架构做调整,需要将数据迁移到MongoDB上去,需要重新实现一个针对MongoDB的Dao泛型类,好吧,动手开工,当实现删除操作的时候问题来了. 我们的删除操作定义如下:voi ...

随机推荐

  1. ZOJ2532_Internship

    一个单源多汇的有向图,求增大那些边的容量可以使得网络的最大流增加. 很简单,直接跑最大流,保留残余网络,然后枚举所有余量为0的边,使其容量增加一个1,看看是否出现新的增广路即可. 召唤代码君: #in ...

  2. Jetty入门

    Jetty的入门 一.开发环境 Eclipse  4.3.1 Maven  3.1 Jetty  9.0.6.v20130930 Jetty的下载地址: http://download.eclipse ...

  3. java对xml文件做增删改查------摘录

    java对xml文件做增删改查 package com.wss; import java.io.File;import java.util.ArrayList;import java.util.Lis ...

  4. python 传值引用和传参引用

    调用同事的函数,传入goods_list,获取商品信息,然后将商品信息与goods_list的信息进行匹配,合并. 但是同事返回数据的同时改变了我传入的参数,goods_list.相当于传参引用,也就 ...

  5. Android的项目不能直接引用可移植类库的项目解决方法

    深圳_exception() 10:25:51 Android的项目不能直接引用可移植类库的项目,但是可以引用可移植类库生成的dll,这就意味着无法直接断电调试可移植类库上海-黄药师() 10:26: ...

  6. nginx实现访问网站或目录密码认证保护

    添加目录登陆认证 location / { auth_basic "提示"; auth_basic_user_file /usr/conf/htpasswd; } auth_bas ...

  7. PCB检查事项,生成钻孔表

    PCB检查事项 检查器件是否都放完, 检查连接线是否全部布完, 检查Dangling Line,Via, 查看铜皮是否孤立和无网络铜皮, 检查DRC, 1.选择菜单Display-Status,查看标 ...

  8. 笔记:Hyper-V上Centos 6.5分辨率调整问题解决笔记

    最近忙的没有心情写东西,果然博客就这么长草了.今天就稍微写一点点东西吧,反正这问题挺烦的. 背景如下:为准备做redis集群实验,特在笔记本上搭建CentOS6.5的Hyper-V虚拟机. 虚拟机创建 ...

  9. SQL Server 利用游标解决Tempdb究极竞争-DBA-程序员需知

    SQL Server tempdb分配竞争算是DBA老生常谈的问题了,几乎现在所有的DBA都知道多建几个文件来解决/缓解问题.但是深层次的的竞争依旧不可避免.这里给大家剖析下游标在tempdb中的特点 ...

  10. RaisingStudio.PackageManager 发布 1.0版

    从此免去命今行打包的痛苦,安装本Module后,可以在Dashbaord的Modules里看到一个Download页,进入可看到全部已安装模块列表(与Installed页内容一致),可搜索找到要打包的 ...