繁体   English   中英

在二维数组中移动值

[英]Shifting values in 2d array

我是编程新手,我正在研究一种方法,该方法将给定列中的所有最高值存储在列底部的二维数组中,我使用 Random 类来获取不同的值。

我被困在那里,现在不知道该怎么做,任何帮助将不胜感激,

到目前为止我的代码

 void StoreHighestValueAtBottom(int[,] matrix, int column)
    {
        int max = 0;
        int maxValue = 0;
        for (int row = 0; row < matrix.GetLength(0); row++)
        {
            if (matrix[row, column] > max)
            {
                max = matrix[row, column];
                Array.Copy(matrix, 0, matrix, matrix.GetLength(0) + 1, matrix.GetLength(0) * matrix.GetLength(1) - matrix.GetLength(0) - 1);
                Array.Copy(new int[matrix.GetLength(0) + 1, 1], max, matrix, max, matrix.GetLength(0) + 1);
            }
        }

    }

目前尚不清楚将要实施的内容:

一种将给定列中所有最高值存储在列底部的二维数组中的方法

如果您想找出给定column中的(单个) max并将此max与列中的底部项目交换,以使max成为列中的最后一个:

 void StoreHighestValueAtBottom(int[,] matrix, int column) {
   int maxRow = 0;
   int max = matrix[0, column];

   // Find max value and the row where it is
   for (int row = 1; row < matrix.GetLength(0); ++row) {
     if (matrix[row, column] > max) {
       max = matrix[row, column];
       maxRow = row; 
     } 
   }
   
   // swap bottom item and max item 
   int bottom = matrix[matrix.GetLength(0) - 1, column];
   matrix[matrix.GetLength(0) - 1, column] = max;
   matrix[maxRow, column] = bottom; 
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM