7

我有10个字。我怎样才能得到 5 个单词的所有可能组合(n=10, k=5)。顺序无所谓

例如:"A", "B", "C", if k=2 (n=3 in this case),它会像 AB、BC 和 AC。也许您知道一些有用的代码或示例。

PS对不起,如果我不够正确,因为我不太懂英语。

4

4 回答 4

25

您要做的是获取集合的所有排列。

这是代码片段:

static void Main(string[] args)
{
    var list = new List<string> { "a", "b", "c", "d", "e" };
    var result = GetPermutations(list, 3);
    foreach (var perm in result)
    {
        foreach (var c in perm)
        {
            Console.Write(c + " ");
        }
        Console.WriteLine();
    }
    Console.ReadKey();
}

static IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<T> items, int count)
{
    int i = 0;
    foreach (var item in items)
    {
        if (count == 1)
            yield return new T[] { item };
        else
        {
            foreach (var result in GetPermutations(items.Skip(i + 1), count - 1))
                yield return new T[] { item }.Concat(result);
        }

        ++i;
    }
}

输出:

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 
于 2013-07-26T02:34:25.843 回答
2

这是我放在一起的:

static class LinqExtensions
{
    public static IEnumerable<IEnumerable<T>> CombinationsWithoutRepetition<T>(
        this IEnumerable<T> items,
        int ofLength)
    {
        return (ofLength == 1) ?
            items.Select(item => new[] { item }) :
            items.SelectMany((item, i) => items.Skip(i + 1)
                                               .CombinationsWithoutRepetition(ofLength - 1)
                                               .Select(result => new T[] { item }.Concat(result)));
    }

    public static IEnumerable<IEnumerable<T>> CombinationsWithoutRepetition<T>(
        this IEnumerable<T> items,
        int ofLength,
        int upToLength)
    {
        return Enumerable.Range(ofLength, Math.Max(0, upToLength - ofLength + 1))
                         .SelectMany(len => items.CombinationsWithoutRepetition(ofLength: len));
    }

}

...

foreach (var c in new[] {"a","b","c","d"}.CombinationsWithoutRepetition(ofLength: 2, upToLength: 4))
{
    Console.WriteLine(string.Join(',', c));
}

产生:

a,b
a,c
a,d
b,c
b,d
c,d
a,b,c
a,b,d
a,c,d
b,c,d
a,b,c,d

请注意,这是简洁但效率低下的,不应该用于大型集合或内部循环。值得注意的是,短数组会被多次重新创建并且可以被记忆,并且IEnumerable会被多次迭代,如果不小心可能会导致意外的工作。

此外,如果输入包含重复项,则输出也将包含重复项。要么.Distinct().ToArray()首先使用,要么使用另一种解决方案,其中包括相等性检查,并且大概需要一个IEqualityComparer通用性。

于 2018-06-25T17:15:08.270 回答
1
public IActionResult Index() 
{
    var list = new List<string> { "a", "b", "c", "d", "e" };
    List<string> ret = GetAllCombinations(list).OrderBy(_ => _).ToList();

    return View();
}

static IEnumerable<string> GetAllCombinations(IEnumerable<string> list)
{
    return list.SelectMany((mainItem, mi) => list.Where((otherItem, oi) => mi < oi)
                              .Select(otherItem => mainItem + otherItem));
}

输出:

ab
ac
ad
ae
bc
bd
be
cd
ce
de
于 2020-11-03T10:53:33.507 回答
0

更实用的解决方案怎么样

var list = new List<string> { "a", "b", "c", "d", "e" };
GetAllCombinations(list).OrderBy(_ => _).ToList().ForEach(Console.WriteLine);


static IEnumerable<string> GetAllCombinations(IEnumerable<string> list)
{
    return list.SelectMany(mainItem => list.Where(otherItem => !otherItem.Equals(mainItem))
                              .Select(otherItem => mainItem + otherItem));
}

输出:

ab
ac
ad
ae
ba
bc
bd
be
ca
cb
cd
ce
da
db
dc
de
ea
eb
ec
ed
于 2017-02-16T21:04:06.317 回答