C# 一个数组集合,任意组合,不遗漏,不重复

Wesley13
• 阅读 584
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace listTst
{
    class Program
    {
        static void Main(string[] args)
        {
            var sw = Stopwatch.StartNew();
            var array = new List<Storage>()
            {
                new Storage{ Id = 1, Name = "A" },
                new Storage{ Id = 2, Name = "B" },
                new Storage{ Id = 3, Name = "C" },
                new Storage{ Id = 4, Name = "D" },
                new Storage{ Id = 5, Name = "E" },
                new Storage{ Id = 6, Name = "F" },
                new Storage{ Id = 7, Name = "G" },
                new Storage{ Id = 8, Name = "H" },
                new Storage{ Id = 9, Name = "I" },
            };

            var result = new List<Group>();
            array.ForEach(a => { result.Add(new Group(a)); });
            for (int count = 2; count <= array.Count; count++)
            {
                Test(result, array, 0, count);
            }
            sw.Stop();

            foreach (var group in result)
            {
                Console.WriteLine(group.Name);
            }
            Console.WriteLine($"组合数量:{result.Count}");
            Console.WriteLine($"耗时:{sw.ElapsedMilliseconds}ms");
            Console.ReadLine();
        }

        static void Test(List<Group> result, List<Storage> array, int begin, int count)
        {
            var list = new List<Storage>();
            var end = begin + count - 1;
            if (end > array.Count) return;
            for (int i = begin; i < end; i++)
            {
                list.Add(array[i]);
            }
            if (list.Count < count)
            {
                for (int index = end; index < array.Count; index++)
                {
                    var group = new Group(list);
                    group.Storages.Add(array[index]);
                    result.Add(group);
                }
            }

            if (++begin < array.Count) Test(result, array, begin, count);
        }

        class Group
        {
            public Group(Storage storage)
            {
                Storages.Add(storage);
            }
            public Group(List<Storage> list)
            {
                Storages.AddRange(list);
            }
            public string Name => string.Concat(Storages.Select(a => a.Name));
            public List<Storage> Storages = new List<Storage>();
        }

        class Storage
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
    }
}

C# 一个数组集合,任意组合,不遗漏,不重复

C# 一个数组集合,任意组合,不遗漏,不重复

C# 一个数组集合,任意组合,不遗漏,不重复

C# 一个数组集合,任意组合,不遗漏,不重复

点赞
收藏
评论区
推荐文章
Stella981 Stella981
3年前
C# CRC16 modbus
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceWaterBubbleCheck{
Stella981 Stella981
3年前
C# HTTPServer和OrleansClient结合
usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.IO.Compression;usingSystem.Linq;usingSystem.Net;usingSystem.Text;
Stella981 Stella981
3年前
C# 中合并2个 Dictionary
内置方法usingSystem.Collections.Generic;usingSystem.Linq;Dictionary<string,objectdicAnewDictionary<string,object(){{"Name","姓名"},
Stella981 Stella981
3年前
Mvc+三层(批量添加、删除、修改)
DAL层usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingModel;usingSystem.Data;usingNe
Easter79 Easter79
3年前
System.Threading.Thread的使用及传递参数等总结
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;namespaceConsoleApplication3{classProgram{stati
Stella981 Stella981
3年前
C#通过HttpListener实现HTTP监听
代码:usingNLog;usingSystem;usingSystem.Diagnostics;usingSystem.IO;usingSystem.Net;usingSystem.ServiceProcess;usingSystem.Text;usingSy
Stella981 Stella981
3年前
C#后台调用webapi
usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Net;usingSystem.Text;usingSystem.Threading.Tasks;
Wesley13 Wesley13
3年前
C#实现自定义栈
使用C自定义栈1.定义一个MyStack泛型类usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Thr
Wesley13 Wesley13
3年前
C# 实数矩阵行列式计算
原文链接: C实数矩阵行列式计算(https://my.oschina.net/ahaoboy/blog/775722)usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSy
Wesley13 Wesley13
3年前
C# 生成二维码
 1、添加ThoughtWorks.QRCode.dll引用 2、代码usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Threading.Tasks;usingMicrosof