数组中 K 个元素的总和,等于 N

本文关键字:等于 元素 数组 | 更新日期: 2023-09-27 17:56:30

给定一个数组,比如 nums = { 1,2,5,3,6,-1,-2,10,11,12},使用最大元素数(假设 maxNums=3)求求总和(说总和 =10)= K 的元素

所以如果要使用的最大数字 = 3 求和 = 10答案是

    {1  3  6}    
    {1  -1  10}
    {1  -2  11}
    {2  5  3}
    {2  -2  10}
    {5 6 -1}
    {-1  11}
    {-2  12}
    {10}

我写了一个递归函数来完成这项工作。如何在没有递归的情况下做到这一点?和/或内存较少?

class Program
{
        static Int32[] nums = { 1,2,5,3,6,-1,-2,10,11,12};
        static Int32 sum = 10;
        static Int32 maxNums = 3;

        static void Main(string[] args)
        {
            Int32[] arr = new Int32[nums.Length];
            CurrentSum(0, 0, 0, arr);
            Console.ReadLine();
        }
        public static void Print(Int32[] arr)
        {
            for (Int32 i = 0; i < arr.Length; i++)
            {
                if (arr[i] != 0)
                    Console.Write("  "   +arr[i]);
            }
            Console.WriteLine();
        }

        public static void CurrentSum(Int32 sumSoFar, Int32 numsUsed, Int32 startIndex, Int32[] selectedNums)
        {
            if ( startIndex >= nums.Length  || numsUsed > maxNums)
            {
                if (sumSoFar == sum && numsUsed <= maxNums)
                {
                    Print(selectedNums);
                }                    
                return;
            }
                       **//Include the next number and check the sum**
                    selectedNums[startIndex] = nums[startIndex];
                    CurrentSum(sumSoFar + nums[startIndex], numsUsed+1, startIndex+1, selectedNums);
                    **//Dont include the next number**
                    selectedNums[startIndex] = 0;
                    CurrentSum(sumSoFar , numsUsed , startIndex + 1, selectedNums);
        }
    }

数组中 K 个元素的总和,等于 N

你的函数看起来不错,但可能有点优化:

class Program
{
    static Int32[] nums = { 1, 2, 5, 3, 6, -1, -2, 10, 11, 12 };
    static Int32 sum = 10;
    static Int32 maxNums = 3;
    static Int32[] selectedNums = new Int32[maxNums];
    static void Main(string[] args)
    {
        CurrentSum(0, 0, 0);
        Console.ReadLine();
    }
    public static void Print(int count)
    {
        for (Int32 i = 0; i < count; i++)
        {
            Console.Write(" " + selectedNums[i]);
        }
        Console.WriteLine();
    }
    public static void CurrentSum(Int32 sumSoFar, Int32 numsUsed, Int32 startIndex)
    {
        if (sumSoFar == sum && numsUsed <= maxNums)
        {
            Print(numsUsed);
        }
        if (numsUsed >= maxNums || startIndex >= nums.Length)
            return;
        for (int i = startIndex; i < nums.Length; i++)
        {
            // Include i'th number
            selectedNums[numsUsed] = nums[i];
            CurrentSum(sumSoFar + nums[i], numsUsed + 1, i + 1);
        }
    }
}

我还修复了您的函数中的一个错误。它在以下测试用例上失败:

{10, 2, -2}
Sum = 10
K = 3

您的函数仅返回 {10} 而不是{10} and {10, 2, -2}

还有Haskell解决方案...

import Data.List
listElements max_num k arr = 
  filter ('x -> sum x == k && length x == max_num) $ subsequences arr

*主>列表元素 3 10 [1,2,5,3,6,-1,-2,10,11,12]
[[

2,5,3],[1,3,6],[5,6,-1],[1,-1,10],[2,-2,10],[1,-2,11]]