C# 用Linq的方式实现组合和笛卡尔积(支持泛型T)

2021-04-19 14:27

阅读:596

转载请留出处《http://www.cnblogs.com/localhost2016/p/8668355.html》

组个例子:用户选择穿衣服组合方案。这个组合方案怎么生成

string[] a = {"帽子1",“帽子2”,“帽子3”};

string[] b = {"外套1",“外套2”};

string[] c ={"裤子1",“裤子2“,“裤子3”,“裤子4”};

string[] d ={"鞋子1",“鞋子2”,“鞋子3”};

起初实现很简单,就是用循环得到所要的组合字符串。但是稍微一变动,哪怕仅仅是类型的改变,就歇菜了。下面我给出我实现的方式。

 1     public static class EnumerableEx
 2     {
 3         /// 
 4         /// 从集合中的选出K个元素组合数
 5         /// 
 6         public static IEnumerable> Combinations(this IEnumerable sequences, int k)
 7         {
 8             return k == 0 ? new[] { new T[0] } : sequences.SelectMany((e, i) =>
 9                                                                        sequences.Skip(i + 1)
10                                                                                 .Combinations(k - 1)
11                                                                                 .Select(c => (new[] { e }).Concat(c))
12                                                                      );
13         }
14         /// 
15         /// 求集合的笛卡尔积
16         /// 
17         public static IEnumerable> Cartesian(this IEnumerable> sequences)
18         {
19             IEnumerable> tempProduct = new[] { Enumerable.Empty() };
20             return sequences.Aggregate(tempProduct,
21                                          (accumulator, sequence) =>
22                                             from accseq in accumulator
23                                             from item in sequence
24                                             select accseq.Concat(new[] { item })
25                                        );
26         }
27     }

调用例子:

组合:从7个元素中选出3个元素。

1             string[] are = { "1", "2", "3", "4", "5", "6", "7" };
2             var cc = are.Combinations(3);
3             foreach (var c in cc)
4             {
5                 Console.WriteLine(string.Join("_", c));
6             }

笛卡尔积:

            Liststring>> items = new Liststring>>()
            {
                new Liststring>(){ "a1","a2","a3"},
                new Liststring>(){ "b4","b5"},
                new Liststring>(){ "c6" }
            };

            foreach (var item in items.Cartesian())
            {
                Console.WriteLine(string.Join(",",item));
            }

 


评论


亲,登录后才可以留言!