【问题标题】:How to find the exact location of a maximum and minimum number in a matrix如何在矩阵中找到最大和最小数字的确切位置
【发布时间】:2016-02-23 00:35:42
【问题描述】:

如何找到矩阵中最大和最小数字的确切位置。该代码可以显示矩阵中的最大值和最小值,但我需要找到确切的位置,例如行和列。需要这样格式化。第 0 行第 3 列的最大数量为 2。

import java.util.Scanner;


public class alarconh_Program2 {

public static void main(String[] args){

Scanner input = new Scanner (System.in);
int row = 0;
int col = 0;

double col1sum,col2sum,col3sum,col4sum,col1avg,col2avg,col3avg,col4avg;

System.out.println(" (Col) x (Row) ");
System.out.println("Enter the number of rows");
row = input.nextInt();
System.out.println("Enter the number of columns");
col = input.nextInt();

double [][] matrix = new double[row][col];

for (int i = 0; i < row; i++){

for(int j = 0; j < col; j++){

matrix [i][j] = input.nextDouble();
}
}
System.out.println();
String formathd="The Matrix is";
System.out.printf(formathd);
for (int i1 = 0; i1 < matrix.length; i1++){

System.out.println();

for(int j1 = 0; j1 < matrix[i1].length; j1++){
String Table=" %2.1f ";
System.out.printf(Table, matrix [i1][j1]);

}

}


col1sum = matrix[0][0] + matrix[1][0] + matrix[2][0];
col2sum = matrix[0][1] + matrix[1][1] + matrix[2][1];
col3sum = matrix[0][2] + matrix[1][2] + matrix[2][2];
col4sum = matrix[0][3] + matrix[1][3] + matrix[2][3];
System.out.println();
String SUM = "%2.1f %2.1f %2.1f %2.1f :SUM";
System.out.printf(SUM,col1sum,col2sum,col3sum,col4sum);

col1avg= col1sum/row;
col2avg = col2sum/row;
col3avg = col3sum/row;
col4avg = col4sum/row;
System.out.println();

String AVG = " %2.1f  %2.1f  %2.1f  %2.1f :AVG";
System.out.printf(AVG,col1avg,col2avg,col3avg,col4avg);





System.out.println();
double maxValue = Integer.MIN_VALUE;

for (int i2 = 0; i2 < matrix.length; i2++)
    for (int j2 = 0; j2 < matrix[i2].length; j2++)
        if (matrix[i2][j2] > maxValue)
           maxValue = matrix[i2][j2];

System.out.println("Maximum numbers is " +maxValue + " at row " +row+ ", and column " +col);




double minValue =Integer.MAX_VALUE;

for (int i2 = 0; i2 < matrix.length; i2++)
    for (int j2 = 0; j2 < matrix[i2].length; j2++)
        if (matrix[i2][j2] < minValue)
           minValue = matrix[i2][j2];
System.out.println("Minimum numbers is " +minValue+  " at row " +row+ ", and column " +col);
}

}

【问题讨论】:

    标签: java matrix


    【解决方案1】:

    如果要打印最小值和最大值的确切位置,请将最后一个循环编辑为:-

    for (int i2 = 0; i2 < matrix.length; i2++)
    for (int j2 = 0; j2 < matrix[i2].length; j2++)
        if (matrix[i2][j2] < minValue)
           {
              minValue = matrix[i2][j2];
              row = i2; // stores the current row to row variable
              col = j2  // stores current columb to col variable
           }
    System.out.println("Minimum numbers is " +minValue+  " at row " +row+ ", and column " +col);
    

    【讨论】:

      【解决方案2】:
      max=m[0][0];
      for(int i=0;i<r;i++)
      {
      for(int j=0;j<c;j++)
      {
      if(m[i][j]>max)
        {
          max=m[i][j];
          row = i;
          col = j;
        }
      }
      }
      System.out.println("Maximum element in  matrix is "+max+" at m["+row+"]["+col+"]");
      //MINIMUM element of the matrix
      min=m[0][0];
      for(int i=0;i<r;i++)
      {
      for(int j=0;j<c;j++)
      {
      if(m[i][j]<min)
        {
         min=m[i][j];
          row = i;
          col = j;
        }
      }
      }
      System.out.println("Minimum elementin  matrix is "+min+" at m["+row+"]["+col+"]");
      

      【讨论】:

        【解决方案3】:

        对于较大的矩阵,O^2 方法的成本将过高。

        还可以在填充矩阵时维护元数据。对于 r(ow) x c(olumn) 矩阵,创建一个大小为 c 的附加单维数组,并且每当将新的最大值放入第 n 行时,使用新最大值的 c 更新索引 n 的附加数组。

        这是内存和处理之间的权衡,但对于不平凡的用例,它可能会更快。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-01-14
          • 2018-08-27
          • 2013-11-20
          • 1970-01-01
          • 1970-01-01
          • 2015-05-09
          • 1970-01-01
          • 2020-10-11
          相关资源
          最近更新 更多