日期:2014-05-17  浏览次数:20874 次

请教一个List<string>或List<int>字符组合的算法
比如:
List<int> intList=new List<int> ();
intList.Add(1);
intList.Add(2);
intList.Add(3);
intList.Add(4);


需要返回一个字符组合集。如下所示:

1
1,2
1,2,3
1,2,3,4
1,3
1,3,4
1,4
2
2,3
2,3,4
2,4
3
3,4
4

这个算法应该怎么写?

谢谢各位高手!

------解决方案--------------------
你这是一个全排列的问题,代码可以参考下面网页上的代码:
http://blog.csdn.net/LCL_data/article/details/5286847
------解决方案--------------------
30多了,搞了1个多小时才搞出来!

static void Main(string[] args)
        {
            List<int> intList = new List<int>();
            intList.Add(1);
            intList.Add(2);
            intList.Add(3);
            intList.Add(4);

            List<string> strList = new List<string>();

            for (int i = 0; i < intList.Count; i++)
            {
                Console.WriteLine(intList[i].ToString());
                GetListMethod(intList, i, i+1);
            }
            
            Console.ReadKey();
        }

        public static void GetListMethod(List<int> intList,int n, int m)
        {
            
            for (int i = m; i < intList.Count; i++)
            {
                string s = intList[n].ToString();
                for (int j = m; j <=i; j++)
                {
                    s += "," + intList[j].ToString();
                    
                }
                Console.WriteLine(s);
            }