Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 根据字典c获取K大小的组合_C#_Dictionary_Combinations - Fatal编程技术网

C# 根据字典c获取K大小的组合

C# 根据字典c获取K大小的组合,c#,dictionary,combinations,C#,Dictionary,Combinations,我有一本这样的字典 Dictionary<int, string> Diccionario = new Dictionary<int, string>(){ {1,"124"} , {4,"1457"} , {7,"478"}, {2,"1235"} , {5,"24568"} , {8,"05789"}, {3,"236"} , {6,"3569"} , {9,"698"}, {0,"0

我有一本这样的字典

Dictionary<int, string> Diccionario = new Dictionary<int, string>(){
        {1,"124"}  ,  {4,"1457"}  ,  {7,"478"},
        {2,"1235"} ,  {5,"24568"} ,  {8,"05789"},
        {3,"236"}  ,  {6,"3569"}  ,  {9,"698"},
        {0,"08"}   
};
因此,如果我得到32个组合,则会是:深绿色或深橙色


不确定我是否在阐明我的观点

经过一些评论,我想我发现你的问题不止一个

迪乔纳里奥应该是一本字典。字符串是一种很难使用的类型。由于您似乎没有使用*和键,我也看不到对char的调用。 即使是一本字典也可能会过火。一个简单的索引集合也可以做到这一点。通常仅当索引不连续时才应使用它。但为了便于阅读,这本词典可以在这里使用。 输入n也不是单个数字,而是int[]的数组 对于输入{3,3,9},需要{2,3,6},{2,3,6}和{6,8,9}的所有组合。使用4+层嵌套循环就可以做到这一点,但您可能希望研究递归。这是一个固有的递归操作


然而,我对命名参数和变量感到非常困惑。这使得编写示例代码非常困难。

您正在尝试做笛卡尔积。这里解决了这个问题:所有的功劳都归于埃里克·利珀特

使用该功能,任务非常简单:

private static readonly int[][] adjacent = new int[][]
    {
        new int[]{0,8},//0
        new int[]{1,2,4},//1  
        new int[]{1,2,3,5},//2
        new int[]{2,3,6},//3
        new int[]{1,4,5,7},//4  
        new int[]{2,4,5,6,8},//5
        new int[]{3,5,6,9},//6
        new int[]{4,7,8},//7
        new int[]{0,5,7,8,9},//8
        new int[]{6,9,8}//9                
    };

public static IEnumerable<IEnumerable<int>> GetCombinations(int[] input)
{
    if (input.Length == 0) { return new int[0][]; }

    return CartesianProduct(input.Select(n => adjacent[n]));
}

将输入中的每个数字替换为相邻数字的数组,然后进行笛卡尔乘积。空输入有一种特殊情况,CartesianProduct函数返回一个空序列,我猜这不是正确的结果。它应该返回空序列。

n=11的结果如何从11到44?其他的开始结果低于n?如果这个最小值与n的值不相关,那么它从何而来?我想只要您为数据提供一些有意义的输入以及创建输出的规则,您就可以自己解决这个问题。目前我不知道这些数字是如何推导出来的。没有11、31、24或所有其他数字。很抱歉耽搁了,我更新了帖子,所以你可以看到数据来自哪里…水平和垂直方向的数字,例如1有2、3,而数字本身没有。1有2和4作为相邻的数字。但除此之外,它正慢慢开始变得有意义。我的回答很贴切:你需要一个决定。我越了解这个问题,这个设计就越是天生错误。迪乔纳里奥应该是一本字典。永远不要把完美的整数转换成字符串。弦很难用。充其量I/O的Nesserry邪恶|和n也应该是int[]。分别为1,1,2,4和3,3,9。你不应该把两个完全独立的、不相关的整数变成一个整数。这是如何回答这个完全不清楚的问题的?“这仅仅是一个列表,列出了一些理解一些概念的相关要点。”希姆布罗姆比尔我特别引用了我提出的问题。这似乎是一个基本的问题。@HimBromBeere我当时在那里占了50%。现在我100%在那里了。@Christopher Wo,现在我想我明白了,至少我知道如何才能让它工作。我甚至不知道如何将嵌套的itteration ForFor转换为递归函数。谢谢@循环和递归之间的Omar转换通常很容易。然而,存在一种罕见的固有递归问题,这种问题不能有效地转换为循环。这听起来像是一个罕见的案例:
private static readonly int[][] adjacent = new int[][]
    {
        new int[]{0,8},//0
        new int[]{1,2,4},//1  
        new int[]{1,2,3,5},//2
        new int[]{2,3,6},//3
        new int[]{1,4,5,7},//4  
        new int[]{2,4,5,6,8},//5
        new int[]{3,5,6,9},//6
        new int[]{4,7,8},//7
        new int[]{0,5,7,8,9},//8
        new int[]{6,9,8}//9                
    };

public static IEnumerable<IEnumerable<int>> GetCombinations(int[] input)
{
    if (input.Length == 0) { return new int[0][]; }

    return CartesianProduct(input.Select(n => adjacent[n]));
}