2016-02-23 86 views
0

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

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

} 

回答

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

没有probs @ShivPatel –

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

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

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

这是内存和处理之间的折衷,但对于非平凡的用例来说,它可能会更快。

相关问题