题目
定义一个二维数组,数组维度由用户输入,通过函数求数组的鞍点 (二维数组的鞍点就是该位置上的元素在该行上是最大而在该列上是最小,数组可能没有鞍点),函数参数分别为指针和引用的传址方式;
相关阅读
完整代码
#include<bits/stdc++.h>
using namespace std;
int n, m;
void inputSaddlePoint1(int **a, int **b){
bool flag = false;
for (int i = 0; i < n; i++) {
a[i][m] = *max_element(a[i], a[i]+m);
}
for (int i = 0; i < m; i++) {
b[i][n] = *min_element(b[i], b[i]+n);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == a[i][m] && a[i][j] == b[j][n]) {
cout << "在第 " << i+1 << " 行第 " << j+1 << " 列 " << a[i][j] << " 是一个鞍点" << endl;
flag = true;
}
}
}
if (!flag)
cout << "不存在鞍点" << endl;
}
void inputSaddlePoint2(int** &a, int** &b){
bool flag = false;
for (int i = 0; i < n; i++) {
a[i][m] = *max_element(a[i], a[i]+m);
}
for (int i = 0; i < m; i++) {
b[i][n] = *min_element(b[i], b[i]+n);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == a[i][m] && a[i][j] == b[j][n]) {
cout << "在第 " << i+1 << " 行第 " << j+1 << " 列 " << a[i][j] << " 是一个鞍点" << endl;
flag = true;
}
}
}
if (!flag)
cout << "不存在鞍点" << endl;
}
int main(){
int **a, **b;
cin >> n >> m;
a = new int *[n];
b = new int *[m];
for (int i = 0; i < n; i++)
a[i] = new int [m+1];
for (int i = 0; i < m; i++)
b[i] = new int [n+1];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
b[j][i] = a[i][j];
}
}
inputSaddlePoint1(a, b);
inputSaddlePoint2(a, b);
}