手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#中实现任意List的全组合算法代码
C#中实现任意List的全组合算法代码
摘要:复制代码代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;...

复制代码 代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace 算法

{

class 全组合算法

{

[Flags]

public enum PersonType

{

Audit = 1,

Child = 2,

Senior = 4

}

public static void Run(string[] args)

{

var lstSource = GetEnumList<PersonType>();

var lstComb = FullCombination(lstSource);

var lstResult = new List<PersonType>();

lstComb.ForEach(item =>

{

lstResult.Add(item.Aggregate((result, source) => result | source));

});

}

public static List<T> GetEnumList<T>()

{

var lst = new List<T>();

foreach (T item in Enum.GetValues(typeof(T)))

{

lst.Add(item);

}

return lst;

}

//全组合算法

public static List<List<T>> FullCombination<T>(List<T> lstSource)

{

var n = lstSource.Count;

var max = 1 << n;

var lstResult = new List<List<T>>();

for (var i = 0; i < max; i++)

{

var lstTemp = new List<T>();

for (var j = 0; j < n; j++)

{

if ((i >> j & 1) > 0)

{

lstTemp.Add(lstSource[j]);

}

}

lstResult.Add(lstTemp);

}

lstResult.RemoveAt(0);

return lstResult;

}

}

}

【C#中实现任意List的全组合算法代码】相关文章:

C#生成注册码的实例代码

C# WORD操作实现代码

C#中执行批处理文件(*.bat)的方法代码

在C#中创建和读取XML文件的实现方法

使用C#实现在屏幕上画图效果的代码实例

如何应用C#实现UDP的分包组包

C# 邮件发送和接收实现代码

C#中调用命令行cmd开启wifi热点的实例代码

C#实现的最短路径分析

C# 撒列实现关键字过滤的实例

精品推荐
分类导航