微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 从集合中创建独特的双打和三元组

我有一个项目的集合(列表)(字符串).此集合中的项目数始终介于0到9之间.

我需要从这个集合中创建对和三元组的所有组合.
物品在双重或三重中的位置无关紧要.所以{1,2}等于{2,1}.

我怎样才能实现这一目标?也许有一些很好的方法通过LINQ做到这一点?

解决方法

在下面的代码中,我使用 linq生成所有唯一的双精度和三元组.我使用字符串总排序的事实.

这会产生所有双打:

string[] items = { "A","B","C","D","E","F","G","H","I","J" };

var combinations = 
    from a in items
    from b in items
    where a.Compareto(b) < 0
    orderby a,b
    select new { A = a,B = b };

foreach(var pair in combinations)
    Console.WriteLine("({0},{1})",pair.A,pair.B);

这会生成所有三元组

string[] items = { "A","J" };

var combinations = 
    from a in items
    from b in items
    from c in items
    where a.Compareto(b) < 0 && b.Compareto(c) < 0
    orderby a,b,c
    select new { A = a,B = b,C = c };

foreach(var triplet in combinations)
    Console.WriteLine("({0},{1},{2})",triplet.A,triplet.B,triplet.C);

更新:有一个通用的解决方案来创建特定长度的所有唯一子集,仍然使用linq.但是,您需要一个可以包含子集的返回类型.我创建了一个简单的类Linkednode,因为对我而言,这与linq结合起来感觉最自然:

void Main()
{
    string[] items = { "A","J" };

    foreach(var combination in CreateCombinations(items,5))
        Console.WriteLine("({0})",combination.ToString());
}

private static IEnumerable<Linkednode> CreateCombinations(string[] items,int length)
{
    if(length == 1)
        return items.Select(item => new Linkednode { Value = item,Next = null });

    return from a in items 
        from b in CreateCombinations(items,length - 1) 
        where a.Compareto(b.Value) < 0
        orderby a,b.Value
        select new Linkednode<T> { Value = a,Next = b };
}

public class Linkednode
{
    public string Value { get; set; }
    public Linkednode Next { get; set; }

    public override string ToString()
    {
        return (this.Next == null) ? Value : Value + "," + Next.ToString();
    }
}

应该很容易实现IEnumerable< string>在Linkednode类上,或以其他方式将Linkednodes转换为List< string>或HashSet< string>.请注意,如果订单不重要,您可以删除a,b.Value的行顺序.

原文地址:https://www.jb51.cc/csharp/91916.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐