C# 一个数组集合,任意组合,不遗漏,不重复
2024-09-04 03:47:22
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 = , Name = "A" },
new Storage{ Id = , Name = "B" },
new Storage{ Id = , Name = "C" },
new Storage{ Id = , Name = "D" },
new Storage{ Id = , Name = "E" },
new Storage{ Id = , Name = "F" },
new Storage{ Id = , Name = "G" },
new Storage{ Id = , Name = "H" },
new Storage{ Id = , Name = "I" },
}; var result = new List<Group>();
array.ForEach(a => { result.Add(new Group(a)); });
for (int count = ; count <= array.Count; count++)
{
Test(result, array, , 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 - ;
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; }
}
}
}
最新文章
- 【KeyCode 键码】
- [linux系统]--搭建ftp服务器并且 创建用户 设置密码
- c++之string.find(string)
- 转: javascript实现全国城市三级联动菜单代码
- 如何在 Linux 中清除缓存(Cache)
- 如何使用CSS3创建一个漂亮的图标
- 使用DbVisualizer 8 连接Oracle数据库
- inline(内联元素)和block(块级元素) 的区别
- 输入整数n(n<;=10000),表示接下来将会输入n个实数,将这n个实数存入数组a中。请定义一个数组拷贝函数将数组a中的n个数拷贝到数组b中。
- 去掉eclipse js 错误提示
- weblogic上部署应用程序
- 告示:CSDN博客通道支持Windows Live Writer写blog离线好友
- cct,web技术
- poj2236(并查集)
- Linux磁盘分区,目录树,文件系统的关系(转)
- C:函数:功能:实现字符数组中所有字母的倒序存放并输出
- 当PsychicBoom_发觉自己是个大SB的时候……
- Ubuntu 安装 Docker CE
- 第16月第10天 poco target
- NDK学习笔记(三):DynamicKnobs的机制
热门文章
- Spark On YARN(Yarn-Cluster模式)启动流程源码分析(二)
- (3)Linux命令分类汇总(7~12)
- pytest.mark.parametrize()参数化应用二,读取json文件
- 【ARM-Linux开发】 uboot启动阶段修改启动参数方法及分析
- leveldb单元测试之宏定义源码剖析
- 主成分分析(PCA)与线性判别分析(LDA)
- (模板)poj3461(kmp模板题)
- 如何查看class文件的编译jdk版本号
- Kubernetes---容器的生命周期
- Web应用中访问WEB-INF下的资源