C#中实现任意List的全组合算法代码_C#教程-查字典教程网
C#中实现任意List的全组合算法代码
C#中实现任意List的全组合算法代码
发布时间:2016-12-28 来源:查字典编辑
摘要:复制代码代码如下: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#教程学习
    热门C#教程学习
    编程开发子分类