【问题标题】:how to get the array containing minimum difference array elements如何获取包含最小差异数组元素的数组
【发布时间】:2017-10-26 13:09:13
【问题描述】:

[Image of the actual problem]

我们必须根据要选择的项目 input3 选择最佳项目。选择应该是我们不总是采取最大项目。相反,我们会选择没有太大区别的项目。

input1: total items
input2: array of items
input3: items to be selected

Scenario 1:
input: 6, {44,55,605,100,154,190}, 1
output should be: {605}

input: 5, {15,85,32,31,2}, 2
output should be: {32,31}

随着我们增加要选择的项目数量,输出应该有更多的项目被选择,差异最小。 以下是我正在尝试的代码,我是新手,请帮助: 我被困在如何使这种动态化。

public static int[] Find(int totalItems, int[] values, int totalToBeSelected)
{
    var i = values;
    int[] results = new int[totalToBeSelected];

    var resultList = new List<int>();

    if (totalToBeSelected == 1)
    {
        resultList.Add(values.Max());
        return resultList.ToArray();
    }
    Array.Sort(i);
    var minmumDiff = (i[0] - i[1]) * -1;

    for (int k = 1; k < i.Length; k++)
    {
        var differnce = i[k] - i[k - 1];

        if (differnce < minmumDiff)
        {
            resultList.Add(i[k]);
            resultList.Add(i[k - 1]);
            minmumDiff = differnce;
        }
    }
    return resultList.ToArray();
}

【问题讨论】:

  • 看起来像是来自 Techgig 的问题,不是吗?
  • 你能发布这个问题的实际问题陈述吗?目前尚不清楚您如何从输入中获得该输出。此外,您为代码提供什么输入,它提供什么输出以及您想要什么输出?你能发一个minimal reproducible example吗?您是否在调试器中单步执行您的代码或添加了打印语句以查看它与您期望的不同之处?
  • @Dukeling 我已经更新了问题,如果您需要更多信息,请告诉我,谢谢
  • 欢迎来到 StackOverflow。请阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。在您发布 MCVE 代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中并重现您描述的问题。
  • 完全不清楚游戏规则是什么。如果它要求您从数组{15,85,32,31,30,28} 中获取三个项目怎么办?预期的结果是什么?如果数组颠倒了怎么办?你应该取差值的绝对值吗?您需要更新您的问题以澄清问题。否则我们甚至无法帮助您设计解决方案

标签: c# arrays algorithm equation-solving


【解决方案1】:

你可以看看这个函数。

    public static int[] Find(int totalItems, int[] values, int totalToBeSelected)
    {
        Array.Sort(values);
        Array.Reverse(values); // We need any value greater than max items diff. Max array item (first item after the sort) enough for it.
        int diff = values[0]; 
        int indx = 0;
        for (int i = 0; i < totalItems - totalToBeSelected +1; i++)
        {
            int temp_diff = values[i] - values[i + totalToBeSelected - 1]; // We are looking for any items group that max and min value difference is minimum 
            if (temp_diff < diff )
            {
                diff = temp_diff;
                indx = i;
            }
        }

        int[] results = new int[totalToBeSelected];
        Array.Copy(values, indx, results, 0, totalToBeSelected);

        return results;
    }

示例:

        Find( 6, new int[] { 44, 55, 605, 100, 154, 190 }, 1 );
        Out: { 605 }

        Find( 5, new int[] { 15, 85, 32, 31, 2 }, 2 );
        Out: { 32, 31 }

【讨论】:

    【解决方案2】:

    问题中的条件不清楚,必须做出一些假设。

    class Program
    {
            static void Main(string[] args)
            {
                var items = new[] {12,14,22,24,6};//new[] { 15, 85, 32, 31, 2};//new[] { 44, 55, 605, 100, 154, 190 };
                var totalItems = items.Count();
                var numberOfItemsToSelect = 3;
    
                var result = Find(totalItems, items, numberOfItemsToSelect);            
    
                PrintList(result);
    
                Console.ReadLine();
            }
    
            static void PrintList(IEnumerable<int> scoreList)
            {
                foreach (var score in scoreList)
                {
                    Console.Write(score);
                    Console.Write(" ");
                }
            }
    
            public static int[] Find(int totalItems, int[]values, int totalTobeSelected)
            {
                var result = new List<int>();
                if (totalTobeSelected <= 1)
                {
                    result.Add(values.Max());
    
                }
                else if (totalTobeSelected == totalItems)
                {
                    result.AddRange(values.OrderBy(i => i).ToList());
                }
                else
                {
    
                    var mainSet = values.OrderBy(i => i).ToList();
                    var setDic = new Dictionary<int, IEnumerable<int>>();
    
                    for (int i = 0; (totalItems - i >= totalTobeSelected); i++)
                    {
                        var set = mainSet.GetRange(i, totalTobeSelected);
    
                        //Inside a set, we choose the difference between the first and the second number
                        // ex: set = {2, 4, 9} => diff = |2-4| = 2.
                        var diff = Math.Abs(set[0] - set[1]);
    
                        // given two sets with the same diff, we select the first one base on the sort order of the main set:
                        // ex: main set = {2,4,8,10}. Both {2,4} and {6,8} have a diff of 2 so we select {2,4}
                        if (setDic.ContainsKey(diff)) continue;
                        setDic.Add(diff, set);
    
                    }
    
                    if (setDic.Count > 0)
                    {
                        var minKey = setDic.Keys.Min();
                        result.AddRange(setDic[minKey]);
                    }
    
                }
                return result.ToArray();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2019-10-26
      • 1970-01-01
      • 2013-05-07
      • 1970-01-01
      • 1970-01-01
      • 2021-09-11
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      相关资源
      最近更新 更多