从2D Arraylist中提取整数,其总和在一个范围之间

时间:2014-07-31 17:38:56

标签: c#

我正在为.NETMF应用程序编写代码,需要创建一个接受2个参数int min, int max的函数,并从生成的2D Arraylist {{1}返回一个整数数组returnedArr } arr内的整数之和将在returnedArrmin之间,并且在调用方法之前我也不知道max的大小或内容是什么。

将选择arr内每个Arraylist只有1个整数。

arr

示例为 public ArrayList GetNums(int min, int max) { //arr will be structured like this //var arr = new ArrayList //{ // new ArrayList {10, 34, 56, 60}, // new ArrayList {3, 23, 56, 78, 65, 42}, // new ArrayList {53, 56, 76} //}; var returnedArr = new ArrayList(); var arr = GenerateArraylistValues(); //solution code here... return returnedArr; } var returnedArr = new ArrayList(); var arr = GenerateArraylistValues(); //solution code here... return returnedArr; } 返回arraylist可以是GetNums(130,140);

另外请记住,我只限于使用Arraylists,因此我无法使用通用列表,我认为某种Arraylist{56,23,53}可以完成这项工作但不能让我了解这是如何工作的,因为我不知道foreach loop的大小将是什么样的

----------- SAMPLE' arr' VALUES ---------

arr

2 个答案:

答案 0 :(得分:0)

如果' arr'使用递归和蛮力,并不是很大:

    public ArrayList GetNums(int min, int max, ArrayList arr)
    {
        var results = new ArrayList();

        //nothing in the arr?
        if (arr.Count == 0)
            return results;

        //arr has only one list inside
        if(arr.Count == 1)
        {
            foreach(int a in (ArrayList)arr[0])
            {
                if(a >= min && a <= max)
                {
                    var r = new ArrayList();
                    r.Add(a);
                    results.Add(r);
                }
            }
            return results;
        }

        //arr has two or more lists inside
        ArrayList firstList = (ArrayList)arr[0];
        ArrayList remainingArr = new ArrayList();
        for(int i = 1; i < arr.Count; i++)
        {
            remainingArr.Add(arr[i]);
        }

        foreach (int a in firstList)
        {
            var tempResults = GetNums(min - a, max - a, remainingArr);
            foreach(ArrayList result in tempResults)
            {
                var newResult = new ArrayList();
                newResult.Add(a);
                newResult.AddRange(result);
                results.Add(newResult);
            }
        }
        return results;
    }

结果是一系列解决方案。您的示例arr的第一个有效解决方案是{10,65,56}

答案 1 :(得分:0)

如果可以接受每次都有相同的最小值结果,那么你可以做的是:

  1. 从每个子数组的最小值开始,
  2. 对剩余值进行排序,跟踪其源子阵列
  3. 并向上移动已排序的值,直到您达到或超过指定的min
  4. 此类功能的一个示例(使用LINQ简洁,并转换回ArrayList)将是这样的:

    public static ArrayList GetNums(int min, int max)
    {
        var arr = GenerateArraylistValues();
    
        // Initialize our results with the minimum values from each array.
        var results = arr.Cast<ArrayList>().Select(sub => sub.Cast<int>().Min()).ToList();
        var others = new List<UnionItem>();
    
        for (int i = 0; i < arr.Count; i++)
        {
            others = new List<UnionItem>(others
                        // Concatenate the arrays together.
                        .Union(((ArrayList)arr[i]).Cast<int>()
                        // Don't need the min value, which we will start at already. (Optional)
                        .Where(val => val != results[i])
                        // Create the UnionItem, to hold the value and the original array source.
                        .Select(val => new UnionItem(val, i))));
        }
    
        // Order our combined values.
        others = new List<UnionItem>(others.OrderBy(val => val.Value));
        using (var next = others.GetEnumerator())
        {
            // Progress through the combined values until we (a) meet or exceed min, or (b) run out of values.
            while ((results.Sum() < min) && (next.MoveNext()))
            {
                // Update the list of result values according to the UnionItem source.
                results[next.Current.Source] = next.Current.Value;
            }
        }
    
        // Once through our calculation, check now if we've successfully met the conditions.
        int sum = results.Sum();
        if (sum >= min && sum <= max)
        {
            return new ArrayList(results);
        }
        else
        {
            // Whatever happens if no valid match.
            return new ArrayList();
        }
    }
    
    private class UnionItem
    {
        public readonly int Value;   // Holds the value from the array.
        public readonly int Source;  // Holds the index of the source array.
        public UnionItem(int value, int source)
        {
            Value = value;
            Source = source;
        }
    }
    

    我已经创建了UnionItem类,只是为了我们可以跟踪每个值的源数组,以正确地替换我们的结果。