首页 > 解决方案 > 二维数组的第二个最小值

问题描述

我想获得二维数组的第二个最小值,我的原始数组包含很多零,我对此无能为力,但我想获得最小值,这就是我想到这个想法的原因,任何人都有线索?我尝试了以下顺序以获得有效的最低值。我只是把它放在啤酒上发布问题的代码,我不需要修改它我只想知道如何获得第二个最小值。

low1 = result1.Cast().Min();

        for (int m = 0; m < Weights.Count; m++)
        {
            int offset = m * ListCranelocations.Count;

            for (int i = 0; i < ListCranelocations.Count; i++)
            {
                for (int j = 0; j < ListPickLocations.Count; j++)
                {
                    double x = ListCranelocations[i].Lat - ListPickLocations[j].Lat;
                    double y = ListCranelocations[i].Lng - ListPickLocations[j].Lng;
                    R1[i] = Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));

                    if ( R1[i] > Clearance )
                    {     
                        result1[i + offset, j] = Weights[m] * R1[i];

                         //Console.WriteLine(result1[i, j]);
                    } 

                }
            }
        }


        for (int m = 0; m < Weights.Count; m++)

        {
            int offset = m * ListCranelocations.Count;

            for (int i = 0; i < ListCranelocations.Count; i++)
            {
                for (int j = 0; j < ListSetlocations.Count; j++)
                {

                    double x = ListCranelocations[i].Lat - ListSetlocations[j].Lat;
                    double y = ListCranelocations[i].Lng - ListSetlocations[j].Lng;
                    R2[i] = Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));

                    if (R2[i] > Clearance )
                    {

                        result2[i + offset, j] = Weights[m] * R2[i];

                        //  Console.WriteLine(result2[i, j]);
                    }


                }

            }
        }

        double low = 0;
        double low1 = 0;
        double low2 = 0;
        double low23 = 0;


        for (int i = 0; i < result1.GetLength(0); i++)
        {
            for (int j = 0; j < result1.GetLength(1); j++)
            {
                for (int k = 0; k < result2.GetLength(0); k++)
                {
                    for (int m = 0; m < result2.GetLength(1); m++)
                    {

                        if (!(result1[i, j] == 0) && !(result2[k, m] == 0))
                        {

                            result3[i, j] = result1[i, j] + "," + result2[k, m];


                            // Console.WriteLine(result3[i, j]);
                          /*  
                            if ((result1[i, j]) > (result2[k, m]))
                            {
                                highestMoment[i, j] = result1[i, j];
                            }
                            else
                            {
                                highestMoment[i, j] = result2[k, m];
                            }
                            */



                            low1 = result1.Cast<double>().Min();
                            low2 = result2.Cast<double>().Min();

                            if (low1 > low2)
                            {
                                low = low1;
                                Index[i, j] = "P";
                            }
                            else if (low1 > low2)
                            {
                                low = low2;
                                Index[i, j] = "S";
                            }

                            counter++;


                        }


                          // Console.WriteLine(highestMoment[i, j]);



                    }
                }

            }

        }

标签: c#arrays

解决方案


你可以使用Linq扩展方法很容易地得到你需要的东西。如您所知,您可以调用Cast<double>将所有项目放入一个IEnumerable<double>,所以现在您可以跟进Distinct,它获取所有唯一数字,然后OrderBy(i => i)对结果进行排序,最后您可以使用Skip跳过第一个值然后FirstOrDefault到之后得到第一个值(所以是倒数第二个数字):

double secondSmallestValue = twoDimensionalArrayOfValues
    .Cast<double>()
    .Distinct()
    .OrderBy(i => i)
    .Skip(1)
    .FirstOrDefault();

如果您for出于某种原因更喜欢循环方法,您可以通过跟踪最小值和次小值来完成类似的操作,然后遍历数组中的每个项目以查看是否找到比当前最小值更小的值。当你这样做时,只需设置secondSmallest = smallestsmallest = currentValue

var smallestValue = int.MaxValue;
var secondSmallestValue = int.MaxValue;

for(int row = 0; row < values.GetUpperBound(0); row++)
{
    for (int col = 0; col < values.GetUpperBound(1); col++)
    {
        var thisValue = values[row, col];

        if (thisValue < smallestValue)
        {
            // Here you have row and col variables if you need to
            // keep track of the indexes at which the items were found
            secondSmallestValue = smallestValue;
            smallestValue = thisValue;
        }
        else if (thisValue < secondSmallestValue)
        {
            secondSmallestValue = thisValue;
        }
    }
}

在上面的代码中,values被定义为一个10x10填充了从0到的随机整数的数组99

int rowCount = 10;
int colCount = 10;
Random rnd = new Random();

int[,] values = new int[rowCount, colCount];

for(int row = 0; row < rowCount; row++)
    for (int col = 0; col < colCount; col++)
        values[row, col] = rnd.Next(0, 100);

推荐阅读