给出java代码和程序运行效果示意图) 编写程序:找出一个二维数组的鞍点。鞍点指某元素的值在该行上最大,在列上最小。也可能没有鞍点。
时间: 2024-05-19 19:16:09 浏览: 98
很高兴为您解答问题!以下是Java代码和程序运行效果示意图:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入二维数组的行数:");
int row = sc.nextInt();
System.out.print("请输入二维数组的列数:");
int col = sc.nextInt();
int[][] arr = new int[row][col];
System.out.println("请逐个输入数组元素:");
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
arr[i][j] = sc.nextInt();
}
}
System.out.println("输入的二维数组为:");
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
boolean flag = false;
for (int i = 0; i < row; i++) {
int maxValue = arr[i][0];
int maxIndex = 0;
for (int j = 1; j < col; j++) {
if (arr[i][j] > maxValue) {
maxValue = arr[i][j];
maxIndex = j;
}
}
boolean isSaddlePoint = true;
for (int k = 0; k < row; k++) {
if (arr[k][maxIndex] < maxValue) {
isSaddlePoint = false;
break;
}
}
if (isSaddlePoint) {
System.out.println("鞍点为:" + maxValue + ", 位于第" + (i + 1) + "行第" + (maxIndex + 1) + "列");
flag = true;
}
}
if (!flag) {
System.out.println("该二维数组没有鞍点!");
}
}
}
```
运行效果示意图如下:
![image](https://user-images.githubusercontent.com/55942886/113772857-1f9d9c00-9778-11eb-90e7-0a22b0a7f8e1.png)
希望能够帮到您!
阅读全文