C# 从M选择N个组合

yhb_spring 2014-10-18 09:32:50
string[] Items = new string[] { "A", "B", "C" };
比如选取2个数据进行组合成:
AB
AC
BC
注:Items 数组数据不固定,选取的组合也是不固定,可以是选取2,3,4,......N个数据进行组合
...全文
806 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2014-10-19
  • 打赏
  • 举报
回复
threenewbee 2014-10-19
  • 打赏
  • 举报
回复
引用 4 楼 xianfajushi 的回复:
[quote=引用 2 楼 caozhy 的回复:] http://bbs.csdn.net/topics/390550326 回答很多次了
有耐心[/quote] google M选N caozhy 排第一的就是。
  • 打赏
  • 举报
回复
引用 2 楼 caozhy 的回复:
http://bbs.csdn.net/topics/390550326 回答很多次了
有耐心
threenewbee 2014-10-19
  • 打赏
  • 举报
回复
引用 8 楼 yhb_spring 的回复:
[quote=引用 3 楼 caozhy 的回复:]
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行。不需要递归。
不是我想要的,要求是从中选取2个数据就是2个数据组合,选取3个就是三个数据 组合 ,选取N个数据 就是N个数据组合,你这个不是[/quote] 无非是输出格式的问题,你想要什么格式自己修改下就是了。
  • 打赏
  • 举报
回复
对于运行效率问题,我你应该自己使用大量数据进行测试。以测试为准,而不是以理论为准。
  • 打赏
  • 举报
回复
#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();
}
  • 打赏
  • 举报
回复
不知道你说的“不是我想要的”是指什么。你重复一遍别人告诉你的“要求”没有用啊,如果你连人家给你的答案为什么不对都说不出来的话。难道你只有别人吧 "1", "2", "3", "4", "5", "6" 改成 "A","B","C" ,然后把 n =3 改为 n= 2,你才肯“抄袭一下”? 不过话说回来,这个确实不对。如果改为 "A","B","B"就错了。原因就是其中的 y.CompareTo(x.First()) < 0) 算法含义有错。
yhb_spring 2014-10-19
  • 打赏
  • 举报
回复
引用 3 楼 caozhy 的回复:
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行。不需要递归。
不是我想要的,要求是从中选取2个数据就是2个数据组合,选取3个就是三个数据 组合 ,选取N个数据 就是N个数据组合,你这个不是
yhb_spring 2014-10-19
  • 打赏
  • 举报
回复
都不是我想要的!
threenewbee 2014-10-18
  • 打赏
  • 举报
回复
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行。不需要递归。
threenewbee 2014-10-18
  • 打赏
  • 举报
回复
http://bbs.csdn.net/topics/390550326 回答很多次了
winnowc 2014-10-18
  • 打赏
  • 举报
回复
这是组合算法,基本思路一般都是递归,需要取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;
    }
}

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧