社会主义小青年的博客

噫吁嚱,危呼高哉!Talk___is___Cheap ,show___ me___ your___ code... >
客官,请打赏

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

转载请留出处《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         /// <summary>
 4         /// 从集合中的选出K个元素组合数
 5         /// </summary>
 6         public static IEnumerable<IEnumerable<T>> Combinations<T>(this IEnumerable<T> 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         /// <summary>
15         /// 求集合的笛卡尔积
16         /// </summary>
17         public static IEnumerable<IEnumerable<T>> Cartesian<T>(this IEnumerable<IEnumerable<T>> sequences)
18         {
19             IEnumerable<IEnumerable<T>> tempProduct = new[] { Enumerable.Empty<T>() };
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             }

笛卡尔积:

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

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

 

posted @ 2018-03-29 10:32  小青年先生  阅读(2412)  评论(0编辑  收藏  举报