小编She*_*per的帖子

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

给定一个数组说nums = {1,2,5,3,6,-1,-2,10,11,12},使用max no of elements(比如maxNums = 3)找到其总和的元素(比如sum = 10)= K.

因此,如果要使用maxNums = 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}
Run Code Online (Sandbox Code Playgroud)

我写了一个递归函数来完成这项工作.如果没有递归,我该怎么做? 和/或内存较少?

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[] …
Run Code Online (Sandbox Code Playgroud)

c# algorithm recursion data-structures

5
推荐指数
1
解决办法
2074
查看次数

标签 统计

algorithm ×1

c# ×1

data-structures ×1

recursion ×1