C# 如何检查数组中的所有概率

C# 如何检查数组中的所有概率,c#,arrays,algorithm,linq,C#,Arrays,Algorithm,Linq,例如,我们有一个整数数组,如 private int[] arr = new[] {10,5,5,6,4,5,7,3,8,2 }; 我们有钥匙 private key =1; 目标是指满足以下规则的所有可能性: 键+[索引列表]=11 例如,输出应如下所示: [0] - [1],[2] - [1],[5] - [2],[5] - [1],[7],[9] - [8],[9] 更清楚地说,我们希望以适当的方式对所有指标组合进行细化 key+arr[0]=11; key+arr[1]+ar

例如,我们有一个整数数组,如

private int[] arr = new[] {10,5,5,6,4,5,7,3,8,2 };
我们有钥匙

private key =1;
目标是指满足以下规则的所有可能性:

键+[索引列表]=11

例如,输出应如下所示:

 [0] - [1],[2] - [1],[5] - [2],[5] - [1],[7],[9] - [8],[9]
更清楚地说,我们希望以适当的方式对所有指标组合进行细化

 key+arr[0]=11;
 key+arr[1]+arr[2] =11;
 key+arr[1]+arr[5]=11;
 key+arr[1]+arr[7]+arr[9]=11;
首先,我尝试林克的方式,没有机会! 第二,我试着用*算法来处理这个问题,但我没有运气。 然后,我尝试使用递归调用来执行此操作,但仍然没有得到正确的结果

我知道这并不复杂,我希望自己能在15分钟内完成。但我在这里真的被绊倒了


感谢您的帮助。

在您的朋友中,这里有一个例子可以帮助您朝正确的方向前进。加入胡椒粉和盐调味

public static IEnumerable<string> GetCombinations(int[] set, int sum, string values)
{
    for (int i = 0; i < set.Length; i++)
    {
        int left = sum - set[i];
        string vals = set[i] + "," + values;
        if (left == 0)
        {
            yield return vals;
        }
        else
        {
            int[] possible = set.Take(i).Where(n => n <= sum).ToArray();
            if (possible.Length > 0)
            {
                foreach (string s in GetCombinations(possible, left, vals))
                {
                    yield return s;
                }
            }
        }
    }
}

数组的所有可能组合都是i=2^n-1,其中n是array.Length。 创建自定义int[]massBits=newint[arr.Length],将i转换为二进制,并仅求和massBits中索引为1的那些元素。只向组合列表中添加那些resultSum等于您的SumofIndex的组合。 代码不干净,但工作正常:

 int[] arr = new[] { 10, 5, 5, 6, 4, 5, 7, 3, 8, 2 };
        int key = 1;
        int sumOfIndexes = 11 - key;

        // Number of combinations (2^n-1)
        int NumOfCombinations =(int) Math.Pow(2, arr.Length) - 1;

        // Result
        List<List<int>> listOfCombinations = new List<List<int>>();

        for (int i = 0; i < NumOfCombinations; i++)
        {

            int[] massBits = new int[arr.Length];
            // Convert index to binary
            int indexOfMass = i;
            for(int j = 0; indexOfMass > 0 ; j++)
            {
                int remainder = indexOfMass % 2;
                indexOfMass /= 2;
                massBits[j] = remainder;
            }
            // Take indexes of elements, which contains 1
            var elementToCombine = massBits.Select((bit, j) => bit == 1 ? j: -1).Where(index=>index != -1).ToList();
            // Sum all elements 
            int resultSum = elementToCombine.Sum(index => arr[index]);
            if (resultSum != sumOfIndexes) continue;
            listOfCombinations.Add(elementToCombine);
        }
        foreach (var list in listOfCombinations)
        {
            foreach (var el in list)
            {
                Console.Write(el + " ");
            }
            Console.WriteLine();
        }

数字11是固定的还是一个参数?arr的最大尺寸是多少?听起来像是一个普通的家庭作业。。作为一个人,你如何做到这一点,告诉你的计算机从那里开始。。这是@BugFinder No man的一个变体。这不是家庭作业-家庭作业项目中linq和a*算法的关系是什么?总之,你需要检查2^arraylength组合。如果您不知道如何/从哪里开始,请尝试二进制方法。数组中的每个元素都可以包含在总和中,也可以不包含0或1。如果你这样想的话,一种可能的方法应该变得显而易见。
 int[] arr = new[] { 10, 5, 5, 6, 4, 5, 7, 3, 8, 2 };
        int key = 1;
        int sumOfIndexes = 11 - key;

        // Number of combinations (2^n-1)
        int NumOfCombinations =(int) Math.Pow(2, arr.Length) - 1;

        // Result
        List<List<int>> listOfCombinations = new List<List<int>>();

        for (int i = 0; i < NumOfCombinations; i++)
        {

            int[] massBits = new int[arr.Length];
            // Convert index to binary
            int indexOfMass = i;
            for(int j = 0; indexOfMass > 0 ; j++)
            {
                int remainder = indexOfMass % 2;
                indexOfMass /= 2;
                massBits[j] = remainder;
            }
            // Take indexes of elements, which contains 1
            var elementToCombine = massBits.Select((bit, j) => bit == 1 ? j: -1).Where(index=>index != -1).ToList();
            // Sum all elements 
            int resultSum = elementToCombine.Sum(index => arr[index]);
            if (resultSum != sumOfIndexes) continue;
            listOfCombinations.Add(elementToCombine);
        }
        foreach (var list in listOfCombinations)
        {
            foreach (var el in list)
            {
                Console.Write(el + " ");
            }
            Console.WriteLine();
        }