解决方案 »

  1.   

    这是组合算法,基本思路一般都是递归,需要取N个元素的组合,就是取一个元素+( N-1)个之后元素的组合。
    代码都来自 http://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from-n如果可以用LINQ,最简单的代码是:public static IEnumerable<IEnumerable<T>> Combinations<T>(this IEnumerable<T> elements, int k)
    {
      return k == 0 ? new[] { new T[0] } :
        elements.SelectMany((e, i) =>
          elements.Skip(i + 1).Combinations(k - 1).Select(c => (new[] {e}).Concat(c)));
    }如果不用LINQ,可以用稍复杂点的枚举器:static IEnumerable<string> Combinations(List<string> characters, int length)
    {
        for (int i = 0; i < characters.Count; i++)
        {
            if (length == 1)
                yield return characters[i];
            else
                foreach (string next in Combinations(characters.GetRange(i + 1, characters.Count - (i + 1)), length - 1))
                    yield return characters[i] + next;
        }
    }
      

  2.   

    http://bbs.csdn.net/topics/390550326回答很多次了
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                int n = 3;
                string[] are = { "1", "2", "3", "4", "5", "6" };
                var result = are.Select(x => new string[] { x });
                for (int i = 0; i < n - 1; i++)
                {
                    result = result.SelectMany(x => are.Where(y => y.CompareTo(x.First()) < 0).Select(y => new string[] { y }.Concat(x).ToArray()));
                }
                foreach (var item in result)
                {
                    Console.WriteLine(string.Join(", ", item));
                }
            }
        }
    }
    关键代码只要2行。不需要递归。
      

  4.   

    有耐心
    google M选N caozhy 排第一的就是。
      

  5.   

    不知道你说的“不是我想要的”是指什么。你重复一遍别人告诉你的“要求”没有用啊,如果你连人家给你的答案为什么不对都说不出来的话。难道你只有别人吧 "1", "2", "3", "4", "5", "6"  改成 "A","B","C" ,然后把 n =3 改为 n= 2,你才肯“抄袭一下”?不过话说回来,这个确实不对。如果改为 "A","B","B"就错了。原因就是其中的 y.CompareTo(x.First()) < 0) 算法含义有错。
      

  6.   

    #1楼的程序书写得太诡异了,(且不管运行效率问题)我宁愿写成比较傻瓜化的程序        static IEnumerable<List<string>> Combinations(string[] characters, int length)
            {
                return Combinations(characters, 0, characters.Length - 1, length);
            }        static IEnumerable<List<string>> Combinations(string[] characters, int start, int end, int length)
            {
                if (end < start)
                    yield break;
                else if (length == 1)
                    for (var i = start; i <= end; i++)
                        yield return new List<string> { characters[i] };
                else
                {
                    foreach (var r in Combinations(characters, start, end - 1, length))
                        yield return r;
                    foreach (var r in Combinations(characters, start, end - 1, length - 1))
                    {
                        r.Add(characters[end]);
                        yield return r;
                    }
                }
            }比如说你可以测试static void Main(string[] args)
    {
        string[] are = { "A", "B", "B" };
        var result = Combinations(are, 2);
        foreach (var x in result)
        {
            foreach (var y in x)
                Console.Write(y);
            Console.WriteLine();
        }
        Console.WriteLine("___________按任意键退出");
        Console.ReadKey();
    }