c# - c# 显示可能的组合
问题描述
我有 20 个花盆,用于创建 2 个花盆分组的时间表。我需要找到可以创建的所有可能的时间表。
// create range of pots
IEnumerable<int> pots = Enumerable.Range(1, 20);
获取所有可能的 2 个锅组:
public DataTable CreateCombinations(IEnumerable<int> pots)
{
DataTable testTable = new DataTable();
testTable.Columns.Add("Pot1");
testTable.Columns.Add("Pot2");
for (int i = 0; i < pots.Count(); i++)
{
for (int j = i + 1; j < pots.Count(); j++)
{
var row = testTable.NewRow();
row["Pot1"] = pots.ElementAt(i);
row["Pot2"] = pots.ElementAt(j);
testTable.Rows.Add(row);
}
}
return testTable;
}
可以从 1,n 组合创建的所有可能计划的列表:
public DataTable CreatePossibleSchedules(DataTable dt)
{
DataTable testTable = new DataTable();
testTable.Columns.Add("Pot1");
testTable.Columns.Add("Pot2");
testTable.Columns.Add("Schedule");
for (var i = 0; i < dt.Rows.Count; i++)
{
if(dt.Rows[i]["Pot1"].ToString() == "1")
{
for (var x = i; x < dt.Rows.Count; x++)
{
string pot1 = dt.Rows[x]["Pot1"].ToString();
string pot2 = dt.Rows[x]["Pot2"].ToString();
string schedule = (i + 1).ToString();
bool exists = testTable.AsEnumerable().Any(row =>
(pot1 == row.Field<string>("Pot1") || pot1 == row.Field<string>("Pot2") ||
pot2 == row.Field<string>("Pot1") || pot2 == row.Field<string>("Pot2")) &&
schedule == row.Field<string>("Schedule"));
if (!exists)
{
var row = testTable.NewRow();
row["Pot1"] = dt.Rows[x]["Pot1"];
row["Pot2"] = dt.Rows[x]["Pot2"];
row["Schedule"] = i + 1;
testTable.Rows.Add(row);
}
}
}
}
return testTable;
}
这给了我 1,2 到 1,20 的可能时间表:
时间表 1:
1,2
3,4
...
19,20
到时间表 20:
1、20
2、3
18、19
我不知道该怎么做就是重新开始获取其余可能的时间表:
时间表 21:
1、2
3、5
4、6
希望这是有道理的,不确定我是否已经很好地解释了这个问题。
解决方案
推荐阅读
- javascript - 如何使用 Cloudflare Worker 的缓存 API 呈现 HTML 字符串?
- amazon-web-services - 删除 AWS Amplify 项目时遇到问题
- node.js - Role Base Curd 操作 NodeJs API (user ,admin,superadmin)
- java - 将基本 oAuth 添加到 Java springboot 项目中
- reactjs - 在组件中使用自定义图像做出反应
- html - 如何在 CSS 网格中设置列的高度以使其可滚动?
- node.js - 如何从 node_modules 中删除不需要的包?npm rm 不起作用
- javascript - 在 Shopify 中选中单选按钮时如何获取 ID 值?
- javascript - Javascript Promise.all 处理大量请求
- reactjs - 在 react.js 中运行“hello-react”程序时出错