c++求找出一个二维数组的“鞍点”,即该位置上的元素在该行上最大,在该列上最小。也可能没有鞍点。要简单版的
时间: 2023-09-09 11:08:05 浏览: 82
C语言程序设计-求一个二维数组中的鞍点
5星 · 资源好评率100%
好的,这里提供一个简单版的 C++ 代码实现。具体思路和上面的回答类似,只是代码更加简洁:
```c++
#include <iostream>
using namespace std;
int main()
{
int a[100][100];
int m, n, i, j, flag = 0;
cout << "Enter the number of rows: ";
cin >> m;
cout << "Enter the number of columns: ";
cin >> n;
cout << "Enter the elements of the matrix: " << endl;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
cin >> a[i][j];
}
}
// 找出鞍点
for(i = 0; i < m; i++)
{
int row_max = a[i][0], col_min = a[0][j];
int col_index = 0;
for(j = 1; j < n; j++)
{
if(a[i][j] > row_max)
{
row_max = a[i][j];
}
}
for(j = 1; j < n; j++)
{
if(a[i][j] < col_min)
{
col_min = a[i][j];
col_index = j;
}
}
if(row_max == col_min)
{
cout << "Saddle Point found at (" << i << ", " << col_index << ")" << endl;
flag = 1;
}
}
if(flag == 0)
{
cout << "The matrix has no saddle point." << endl;
}
return 0;
}
```
希望这个简单版的代码能够满足您的需求。
阅读全文