如何选择数组中每行的最高值?

时间:2014-04-20 23:17:33

标签: c# visual-studio-2012

我正在使用Visual Studio 2012处理Windows窗体应用程序。假设我有一个尺寸为M * N的数组,如何选择数组中每行的最大值?

2 个答案:

答案 0 :(得分:0)

遍历所有行,并在每行循环遍历行本身并找到最大值:

function int[] GetMaxValues(int[] Arr)
{
    int[] Max = new int[Arr.GetLength(0)];

    for (int i = 0; i < Arr.GetLength(0); i++)
    {
        Max[i] = int.MinValue;
        for (int l = 0; l < Arr.GetLength(1); l++)
            if (Arr[i, l] > Max[i])
                Max[i] = Arr[i, l];
    }

    return Max;
}

答案 1 :(得分:0)

public static int[] GetMaxValues(int[,] Arr)
{
    int[] Max = new int[Arr.GetLength(0)];
    for (int i = 0; i < Arr.GetLength(0); i++)
    {
        Max[i] = int.MinValue;
        for (int l = 0; l < Arr.GetLength(1); l++)
            if (Arr[i, l] > Max[i])
                Max[i] = Arr[i, l];
    }
    return Max;
}