题目描述
找出一个二维数组中的鞍点,即该位置上的元素在该行上最大,在该列上最小,二维数组的行列n,m以及二维数组均由键盘输入,当存在鞍点时,输出位置以及该点的值,当不存在鞍点时,输出-1
例如输入:
4 5
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
输出:
0 4 5
#include<iostream>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int** a = new int* [n];
for (int i = 0; i < n; i++) {
a[i] = new int[m];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
}
}
bool find;
int ani = 0, anj = 0;
for (int i = 0; i < n; i++) {
ani = i;
anj = 0;
for (int j = 0; j < m; j++)
if (a[i][j] > a[i][anj])
anj = j;
find = true;
for (int k = 0; k < n; k++)
if (a[k][anj] < a[ani][anj]) {
find = false;
}
if (find) {
break;
}
}
if (find) {
cout << ani << " " << anj << " " << a[ani][anj] << endl;
}
else
cout << "-1" << endl;
return 0;
}