全组合算法

//全组合算法
        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;
        }

 

public class test
    {
        public string mountNo;
        public int price = 0;
    }

 

 var lstTest = new List<test>();
            lstTest.Add(new test(){mountNo="A",price=1});
            lstTest.Add(new test() { mountNo = "B", price = 2 });
            lstTest.Add(new test() { mountNo = "C", price = 3 });
            lstTest.Add(new test() { mountNo = "D", price = 4 });
            lstTest.Add(new test() { mountNo = "E", price = 5 });

            var lstCombTest = FullCombination(lstTest);

 

 

http://www.jb51.net/article/37362.htm

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。