手机
当前位置:查字典教程网 >编程开发 >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#版ftp方法实现类的代码

C# 实现简单打印的实例代码

c# n个数排序实现代码

C# 无需COM组件创建快捷方式的实现代码

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

C#中实现网段扫描的代码

c# 实现MD5,SHA1,SHA256,SHA512等常用加密算法源代码

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

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

C# WinForm中Panel实现用鼠标操作滚动条的实例方法

精品推荐
分类导航