C#高级排列场景

标签 c# permutation combinations cartesian-product

我想知道如何根据以下信息找到所有组合:

我从一个 JSON 数据集开始:

var choices = { 1: {'Q': 100, 'R': 150, 'W' : 250, 'T', 30},
                2: {'Q': 90, 'R': 130, 'W' : 225, 'T', 28},
                3: {'Q': 80, 'R': 110, 'W' : 210, 'T', 25},
                4: {'Q': 70, 'R': 90, 'W' : 180, 'T', 22},
                5: {'Q': 60, 'R': 70, 'W' : 150, 'T', 18},
                6: {'Q': 50, 'R': 50, 'W' : 110, 'T', 15},
                7: {'Q': 40, 'R': 30, 'W' : 80, 'T', 11},
                8: {'Q': 30, 'R': 25, 'W' : 50, 'T', 8},
                9: {'Q': 20, 'R': 10, 'W' : 25, 'T', 5},
                10: {'Q': 10, 'R': 5, 'W' : 15, 'T', 3}
              };

我想弄清楚的是如何获取此数据集,并在为每一行选择“Q”、“R”、“W”或“T”元素时生成所有可能的组合。

所以我希望我的最终结果是这样的

var allChoices = { 0: {1: {'Q': 100},
                       2: {'R': 130},
                       3: {'W' : 210},
                       4: {'W' : 180},
                       5: {'T', 18},
                       6: {'R': 50,},
                       7: {'Q': 40,},
                       8: {'T', 8},
                       9: {'R': 10},
                      10: {'W' : 15},
                     },
                 1: {...},
                 ...
                 1048576: {...}

              };

我使用 JSON 是因为我认为它最容易可视化,但有谁知道我如何在 C# 中实现这一点?

如果这还不够清楚,请告诉我,我很难弄清楚如何提出这个问题。

最佳答案

这是一个以 4 为基数的 10 位数字。

class Program
{
    static void Main(string[] args)
    {
        int baseN = 4;
        int maxDigits = 10;
        var max = Math.Pow(baseN, maxDigits);
        for (int i = 0; i < max; i++)
        { // each iteration of this loop is another unique permutation
            var digits = new int[maxDigits];
            int value = i;
            int place = digits.Length - 1;
            while (value > 0)
            {
                int thisdigit = value % baseN;
                value /= baseN;
                digits[place--] = thisdigit;
            }

            int choice = 0;
            foreach (var digit in digits)
            {
                choice ++;
                //Console.Write(digit);
                switch (digit)
                {
                    case 0: break; //choose Q from choice
                    case 1: break; //choose R from choice
                    case 2: break; //choose W from choice
                    case 3: break; //choose T from choice
                }
            }
            //Console.WriteLine();
            // add it to your list of all permutations here
        }
        Console.WriteLine("Done")
        Console.ReadLine();
    }
}

关于C#高级排列场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9314681/

相关文章:

c# - 在 C# 中使用证书的 M2MQtt 连接

C#计算解析器

javascript - 将 ViewBag 数据加载到 Chart.js 多图表中

c# - Windows 窗体 C# 中的打印面板

python - 使用数组和组合模式查找组合

r - 在 2 个向量中查找常见的 "subchains"

python - 具有重叠值的列表的排列

python - 'yield' 在此排列生成器中如何工作?

java - 检索特定组合

c# - 如何将组合硬编码片段转换为递归函数?