gpt4 book ai didi

java - 尝试获取数组中的最大值/最小值

转载 作者:太空宇宙 更新时间:2023-11-04 08:21:39 26 4
gpt4 key购买 nike

我试图获取最大值所在行的列中的最小值。我尝试使用 for 循环,但它不起作用。

假设有一个矩阵 A:

3 4 5
2 3 4
1 2 3

我希望程序在第 [0] 行中找到最大值,然后在最大值的列中找到最小值。所以结果应该是:行 [0] 的最大值 = 5,列 [2] 的最小值 = 3。然后我希望它对所有行执行相同的操作,这就是为什么我使用 while 循环。

这是矩阵:

public int[][] createMatrix(int a, int b){
Scanner inputm = new Scanner(System.in);
A = new int[a][b];
System.out.println("Enter elements for matrix A : ");
for (int i=0 ; i < A.length ; i++){
System.out.println("Enter numbers for " + i +"th row");
for (int j=0 ; j < A[i].length ; j++){
A[i][j] = inputm.nextInt();

}

}
return A;
}

public int[][] displayMatrix(){
System.out.println("Matrix A: ");

for (int i=0 ; i < A.length ; i++)
{ System.out.println();
for (int j=0 ; j < A[i].length ; j++){
System.out.print(A[i][j]+" ");
}
}


return A;

}


public int getMaximumOfEveryRow (int c){
a=c;
int i= 0;
int j;
while(i < A[a].length){

max = Integer.MIN_VALUE;
for ( j = 0; j < A [ i ].length; j++ )
if ( A [ i ] [ j ] > max ){
max = A [ i ] [ j ];
}



for ( i = 0; i < A [ i ].length; i++ )// e
if ( A [ i ] [ j ] < min ){
min = A [ i ] [ j ];
}

System.out.println( "\n Maximum of row " + j + " = " + max );
System.out.println( "Minimum of column " + i + " = " + min );
if(max == min){
System.out.println( min+ " = " + max );
System.out.println( "This is a saddle point. ");
}




i++;

}
return max;


}

这是我到目前为止所拥有的:

public int getMaximumOfEveryRow (int c){ 
a=c;
int i= 0;
int j;
while(i < A[a].length){

max = Integer.MIN_VALUE;
for ( j = 0; j < A [ i ].length; j++ )
if ( A [ i ] [ j ] > max ){
max = A [ i ] [ j ];
}


int e = j;
int r;
for ( i = 0; i < A [ i ].length; i++ )// e
if ( A [ i ] [ j ] < min ){
min = A [ i ] [ j ];
}

System.out.println( "\n Maximum of row " + j + " = " + max );
System.out.println( "Minimum of column " + i + " = " + min );
if(max == min){
System.out.println( min+ " = " + max );
System.out.println( "This is a saddle point. ");
}




i++;

}
return max;


}
public int getMaximumOfEveryColumn ()
{
for ( int i = 0; i < A.length; i++ )
{
maxc = Integer.MIN_VALUE;
for ( int j = 0; j < A [ i ].length; j++ )
if ( A [ j ] [ i ] > maxc )
maxc = A [ j ] [ i ];
System.out.println( "Maximum of column " + i + " = " + maxc );
}
return maxc;
}

public int getMinimumOfEveryColumn_(){
for ( int i = 0; i < A.length; i++ )
{
minc = Integer.MAX_VALUE;
for ( int j = 0; j < A [ i ].length; j++ )
if ( A [ j ] [ i ] < minc )
minc = A [ j ] [ i ];
System.out.println( "Minimum of column " + i + " = " + minc );
}
return minc;
}
public int getMaximumOfEveryRow ()
{
for ( int i = 0; i < A.length; i++ )
{
maxr = Integer.MIN_VALUE;
for ( int j = 0; j < A [ i ].length; j++ )
if ( A [ i ] [ j ] > maxr )
maxr = A [ i ] [ j ];
System.out.println( "Maximum of row " + i + " = " + maxr );
}
return maxr;
}

用于查找列中最大值,然后在该最大值所在行中查找最小值的代码。

public void get_max_of_the_row_of_local_min ()
{
for ( int i = 0; i < A.length; i++ )
{
min = Integer.MAX_VALUE;
max = Integer.MIN_VALUE;
int index_of_min_in_its_col = 0;



//maxc = Integer.MIN_VALUE;
for ( int j = 0; j < A [ i ].length; j++ )

if ( A [ j ] [ i ] > max ){
max = A [ j ] [ i ];
index_of_min_in_its_col = i;
System.out.println( " Maximum of col [" + i + "] = " + max);
}

for ( int j = 0; j < A [ index_of_min_in_its_col ].length; j++ )

if ( A [ index_of_min_in_its_col ] [ index_of_min_in_its_col ] < min ){
min = A [ index_of_min_in_its_col ] [ index_of_min_in_its_col ];
a =j;
}
//System.out.print( " Maximum of col [" + j + "] = " + max);
System.out.println( " Minimum of rol [" + index_of_min_in_its_col + "] = " + min );

if(max == min){
System.out.println("This is a saddle point.");
}

}

最佳答案

解决方案如下:

public void get_minimum_of_the_column_of_local_maximum ()
{
for ( int i = 0; i < A.length; i++ )
{
min = Integer.MAX_VALUE;
max = Integer.MIN_VALUE;
int index_of_maximum_in_its_row = 0;

for ( int j = 0; j < A [ i ].length; j++ )
if ( A [ i ] [ j ] > max )
{
max = A [ i ] [ j ];
index_of_maximum_in_its_row = j;
}

for ( int j = 0; j < A [ index_of_maximum_in_its_row ].length; j++ )
if ( A [ j ] [ index_of_maximum_in_its_row ] < min )
min = A [ j ] [ index_of_maximum_in_its_row ];

System.out.print( " Maximum of row [" + i + "] = " + max);
System.out.println( " Minimum of column [" + index_of_maximum_in_its_row + "] = " + min );
}
}

这段代码有什么作用?

您有 min = Integer.MAX_VALUE, max = Integer.MIN_VALUE 变量。我们将这些值分配给这些数字的原因是为了使找到最大值/最小值成为可能。如何?最初,min 具有最大的整数值,但只要我们发现小于该值的值,我们就会更新最小值,以便 min 的值减小。与 max 变量相同的技术,但当然它会通过比较后增加值来实现另一个方向。

第一个内部 for 循环确定行 i 的最大值,然后使用变量 index_of_maximum_in_its_row 标记该行中最大值的索引。稍后需要在第二个内循环中使用它。

第二个内部循环通过对该列进行一次迭代来确定列号 index_of_maximum_in_its_row 的最小值。然后该方法打印结果。

关于java - 尝试获取数组中的最大值/最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9424544/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com