如何在矩阵中找到最大和最小数字的确切位置

时间:2016-02-23 00:35:42

标签: java matrix

如何在矩阵中查找最大和最小数字的确切位置。代码可以在矩阵中显示最大值和最小值,但我需要找到确切的位置,例如行和列。需要像这样格式化。第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);
}

}

3 个答案:

答案 0 :(得分:0)

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

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);

答案 1 :(得分:0)

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+"]");

答案 2 :(得分:0)

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

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

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