解决方案 »

  1.   

    写个不用递归的:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string metachars = "abcde";
                for (int i = 1; i <= metachars.Length; i++)
                {
                    foreach (string s in foo(metachars, i))
                    {
                        Console.WriteLine(s);
                    }
                }
            }        static IEnumerable<string> foo(string metachars, int i)
            {
                var query = metachars.Select(x => x.ToString().AsEnumerable());
                while (query.First().Count() < i)
                    query = query.SelectMany(x => metachars.Where(y => y > x.Last()).Select(y => x.Concat(y.ToString().AsEnumerable())));
                return query.Select(x => string.Join(",", x));
            }
        }
    }a
    b
    c
    d
    e
    a,b
    a,c
    a,d
    a,e
    b,c
    b,d
    b,e
    c,d
    c,e
    d,e
    a,b,c
    a,b,d
    a,b,e
    a,c,d
    a,c,e
    a,d,e
    b,c,d
    b,c,e
    b,d,e
    c,d,e
    a,b,c,d
    a,b,c,e
    a,b,d,e
    a,c,d,e
    b,c,d,e
    a,b,c,d,e
    请按任意键继续. . .
      

  2.   

    http://bbs.csdn.net/topics/390770707我曾经问过类似的问题且看二楼回答
      

  3.   

            static List<string> GetChildCollections(string strTemp)
            {
                List<string> lstTemp = strTemp.Split(',').ToList();
                var subsets = from m in Enumerable.Range(0, 1 << lstTemp.Count)
                              select (from i in Enumerable.Range(0, lstTemp.Count)
                                   where (m & (1 << i)) != 0
                                   select lstTemp[i]).ToArray();
                return subsets.Select(x => string.Join(",", x)).ToList();
            }
      

  4.   


    啊,大神,你太牛了,
    可是我的字符串是以","号分割的,如“a,b,c,d,e”你这个我都不会改啊。能不能帮改下
      

  5.   

    先学习 linq 基本概念。否则让别人“打注释”,只会是浪费。
      

  6.   


            static List<string> GetChildCollections2(string strTemp)
            {
                List<string> lstTemp = strTemp.Split(',').ToList();
                return Enumerable.Range(0, 1 << lstTemp.Count).Select(x => Enumerable.Range(0, lstTemp.Count).Select(y => (x & (1 << y)) != 0 ? lstTemp[y] : null).Where(z => z != null)).Select(x => string.Join(",", x)).ToList();
            }
      

  7.   


    啊,大神,你太牛了,
    可是我的字符串是以","号分割的,如“a,b,c,d,e”你这个我都不会改啊。能不能帮改下
    string metachars = "abcde";
    ->
    string metachars = "a,b,c,d,e".Replace(",", "");
      

  8.   


    看似简短。。很难理解。。我看了15分钟才懂。。
     static void Main(string[] args)
            {
                string metachars = "abcde";
                for (int i = 1; i <= metachars.Length; i++)
                {
                    foreach (string s in foo(metachars, i))
                    {
                        Console.WriteLine(s);
                    }
                }
                Console.ReadKey();
            }        static IEnumerable<string> foo(string metachars, int place)
            {
                IEnumerable<string> list = metachars.Select(x => x.ToString());
                List<IEnumerable<string>> source = new List<IEnumerable<string>>();
                IEnumerable<string> result = new List<string>();
                int index = 0;
                do
                {
                    source.Clear();
                    for (int i = 0; i < place; i++)
                    {
                        source.Add(list.Skip(index + i).Take(1));
                    }
                    source.Add(list.Skip(place + index));
                    var a = source.Aggregate((thisCurrent, nextCurrent) => (thisCurrent.SelectMany(x => nextCurrent.Select(y => x + y))));
                    result = result.Concat(a);
                    index++;
                }
                while (list.Count() - place > index);
                return result;
            }感觉我这个好懂点。。也好读
      

  9.   


    看似简短。。很难理解。。我看了15分钟才懂。。
     static void Main(string[] args)
            {
                string metachars = "abcde";
                for (int i = 1; i <= metachars.Length; i++)
                {
                    foreach (string s in foo(metachars, i))
                    {
                        Console.WriteLine(s);
                    }
                }
                Console.ReadKey();
            }        static IEnumerable<string> foo(string metachars, int place)
            {
                IEnumerable<string> list = metachars.Select(x => x.ToString());
                List<IEnumerable<string>> source = new List<IEnumerable<string>>();
                IEnumerable<string> result = new List<string>();
                int index = 0;
                do
                {
                    source.Clear();
                    for (int i = 0; i < place; i++)
                    {
                        source.Add(list.Skip(index + i).Take(1));
                    }
                    source.Add(list.Skip(place + index));
                    var a = source.Aggregate((thisCurrent, nextCurrent) => (thisCurrent.SelectMany(x => nextCurrent.Select(y => x + y))));
                    result = result.Concat(a);
                    index++;
                }
                while (list.Count() - place > index);
                return result;
            }感觉我这个好懂点。。也好读
    好懂是相对的。
    你让一个根本不会写程序的人看你觉得好懂的程序,他花1个小时未必能懂。
    相反,这次你花15分钟看懂了,下次15秒钟就够了。