我有以下代码生成一组数字的所有可能的组合。C#组合过滤器
此代码生成组合如下{1 2 3 4 5 6},{1 2 3 4 5 7},{1 2 3 4 5 8} .....等
但我需要应用一些特定的过滤器。
例如,在任意组合第一位数应为“偶数”,第二个数字应该是“奇”等
我想在一个组合中的所有6个数字应用滤镜。任何人都可以帮忙。谢谢。
我有以下代码生成一组数字的所有可能的组合。C#组合过滤器
此代码生成组合如下{1 2 3 4 5 6},{1 2 3 4 5 7},{1 2 3 4 5 8} .....等
但我需要应用一些特定的过滤器。
例如,在任意组合第一位数应为“偶数”,第二个数字应该是“奇”等
我想在一个组合中的所有6个数字应用滤镜。任何人都可以帮忙。谢谢。
筛选结果我想你没有访问Combinatorics.Combinations方法的来源。所以,你只能使过滤器在你的foreach
喜欢的东西:
foreach (int[] combination in Combinatorics.Combinations(values, 6))
{
// first filter
if (combinations[0] % 2 == 0) // first digit should be even
{
// only now should the check be continued (second filter)
if (combinations[1] % 2 != 0) // ... odd
{
// and so on...
if (combinations[2] == someFilter)
{
// you should nest the "ifs" until "combinations[5]" and only
// in the most inner "if" should the number be shown:
string combination = string.format("{{0} {1} {2} {3} {4} {5}}", combinations[0], combinations[1], combinations[2], combinations[3], combinations[4], combinations[5]);
Console.WriteLine(combination);
}
}
}
}
不要使用图片显示的代码。如果您将代码粘贴到问题中,则会自动突出显示语法。 –