C# 如何找到满足条件的确切数字?

C# 如何找到满足条件的确切数字?,c#,backtracking,C#,Backtracking,数组中有给定数量的整数。让我们说:{1,2,3,4,5,6,7}。请注意,这只是一个示例,它们可能不是连续的数字 我需要找到满足以下条件的数字: 这些数字有固定的总和。 应指定数字的计数。 根据上面给出的数字: 如果总和为7,计数为2,则应输出{1,6}。 如果和为7,计数为3,则应输出{1,2, 4}. 如果总和为7,计数为1,则应输出{7}。 我发现了类似的线索:。但是,那里的算法没有指定数字计数的要求。以下是算法,这要归功于: 有人能帮我修改一下,增加额外的数字计数条件吗?谢谢和他的帖子。

数组中有给定数量的整数。让我们说:{1,2,3,4,5,6,7}。请注意,这只是一个示例,它们可能不是连续的数字

我需要找到满足以下条件的数字:

这些数字有固定的总和。 应指定数字的计数。 根据上面给出的数字:

如果总和为7,计数为2,则应输出{1,6}。 如果和为7,计数为3,则应输出{1,2, 4}. 如果总和为7,计数为1,则应输出{7}。 我发现了类似的线索:。但是,那里的算法没有指定数字计数的要求。以下是算法,这要归功于:

有人能帮我修改一下,增加额外的数字计数条件吗?

谢谢和他的帖子。这是对他的代码的简单修改

    private static void GetSumsRecursively(
        List<int> numbers,
        int sum, 
        List<int> candidates, 
        int numbersCount,
        List<List<int>> results)
    {
        int candidateSum = candidates.Sum(x => x);

        if (candidateSum == sum && candidates.Count == numbersCount)
        {
            results.Add(candidates);
        }

        if (candidateSum >= sum)
            return;

        for (int i = 0; i < numbers.Count; i++)
        {
            var remaining = new List<int>();

            for (int j = i + 1; j < numbers.Count; j++)
            {
                remaining.Add(numbers[j]);
            }

            var filteredCandidates = new List<int>(candidates) { numbers[i] };

            GetSumsRecursively(remaining, sum, filteredCandidates,
                numbersCount, results);
        }
    }

    public static List<List<int>> GetNumbers(
        List<int> numbers,
        int numbersCount, 
        int sum)
    {
        if (numbers == null) throw new ArgumentNullException("numbers");

        var results = new List<List<int>>();

        // Fail fast argument validation
        if (numbersCount < 1 ||
            numbersCount > numbers.Count /*||
            sumDifficulty < numQuestions * Question.MinDifficulty ||
            sumDifficulty > numQuestions * Question.MaxDifficulty*/)
        {
            return results;
        }

        // If we only need single questions, no need to do any recursion
        if (numbersCount == 1)
        {
            results.AddRange(numbers.Where(q => q == sum)
                .Select(q => new List<int> { q }));

            return results;
        }

        // We can remove any questions who have a difficulty that's higher
        // than the sumDifficulty minus the number of questions plus one
        var candidateQuestions =
            numbers.Where(q => q <= sum - numbersCount + 1)
                .ToList();

        if (!candidateQuestions.Any())
        {
            return results;
        }

        GetSumsRecursively(candidateQuestions, sum, new List<int>(),
            numbersCount, results);

        return results;
    }

{7}如何将计数从1改为7?这是一个错误还是你使用了一些非标准的计数含义?他可能是指numbercount=1在你的例子1中,如果总和为7,计数为2,它是否也会输出{5,2}和{4,3}?谢谢鲁弗斯。我在5分钟内完成了你的帖子:这是解决方案。
    private static void GetSumsRecursively(
        List<int> numbers,
        int sum, 
        List<int> candidates, 
        int numbersCount,
        List<List<int>> results)
    {
        int candidateSum = candidates.Sum(x => x);

        if (candidateSum == sum && candidates.Count == numbersCount)
        {
            results.Add(candidates);
        }

        if (candidateSum >= sum)
            return;

        for (int i = 0; i < numbers.Count; i++)
        {
            var remaining = new List<int>();

            for (int j = i + 1; j < numbers.Count; j++)
            {
                remaining.Add(numbers[j]);
            }

            var filteredCandidates = new List<int>(candidates) { numbers[i] };

            GetSumsRecursively(remaining, sum, filteredCandidates,
                numbersCount, results);
        }
    }

    public static List<List<int>> GetNumbers(
        List<int> numbers,
        int numbersCount, 
        int sum)
    {
        if (numbers == null) throw new ArgumentNullException("numbers");

        var results = new List<List<int>>();

        // Fail fast argument validation
        if (numbersCount < 1 ||
            numbersCount > numbers.Count /*||
            sumDifficulty < numQuestions * Question.MinDifficulty ||
            sumDifficulty > numQuestions * Question.MaxDifficulty*/)
        {
            return results;
        }

        // If we only need single questions, no need to do any recursion
        if (numbersCount == 1)
        {
            results.AddRange(numbers.Where(q => q == sum)
                .Select(q => new List<int> { q }));

            return results;
        }

        // We can remove any questions who have a difficulty that's higher
        // than the sumDifficulty minus the number of questions plus one
        var candidateQuestions =
            numbers.Where(q => q <= sum - numbersCount + 1)
                .ToList();

        if (!candidateQuestions.Any())
        {
            return results;
        }

        GetSumsRecursively(candidateQuestions, sum, new List<int>(),
            numbersCount, results);

        return results;
    }