我有以下代碼生成一組數字的所有可能的組合。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);
}
}
}
}
不要使用圖片顯示的代碼。如果您將代碼粘貼到問題中,則會自動突出顯示語法。 –