首页 > 编程语言 > 详细

(转)C#全排列组合算法

时间:2017-03-16 15:07:01      阅读:660      评论:0      收藏:0      [点我收藏+]

全排列组合算法方法:

public static List<List<T>> FullCombination<T>(List<T> lstSource)
{
var n = lstSource.Count; var max = 1 << n;//1乘以2的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++) { //i除以2的j次方 if ((i >> j & 1) > 0) { lstTemp.Add(lstSource[j]); } } lstResult.Add(lstTemp); } lstResult.RemoveAt(0); return lstResult; }

测试代码:

List<string> lst = new List<string>() { "a", "b", "c" };
List<List<string>> ll = FullCombination(lst);

for (int i = 0; i < ll.Count; i++)
{
    for (int j = 0; j < ll[i].Count; j++)
    {
        Console.Write(ll[i][j]);
    }
    Console.WriteLine();
}

结果:

技术分享

 

(转)C#全排列组合算法

原文:http://www.cnblogs.com/guwei4037/p/6559507.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!