写一段求二维整型数组的鞍点的c语言代码
时间: 2024-05-08 08:19:19 浏览: 118
以下是求二维整型数组鞍点的C语言代码:
```c
#include <stdio.h>
#define ROW 3
#define COLUMN 3
void saddlePoint(int mat[][COLUMN]) {
int i, j, k;
bool flag = false;
for (i = 0; i < ROW; i++) {
// find the minimum value in row i
int min = mat[i][0];
for (j = 1; j < COLUMN; j++) {
if (min > mat[i][j]) {
min = mat[i][j];
}
}
// check if the minimum is maximum in its column
for (k = 0; k < ROW; k++) {
if (min < mat[k][j]) {
break;
}
}
// mat[i][j] is a saddle point
if (k == ROW) {
printf("Saddle point found at (%d, %d): %d", i, j, mat[i][j]);
flag = true;
}
}
if (!flag) {
printf("No saddle point found in the given matrix.");
}
}
int main() {
int mat[ROW][COLUMN] = {{2, 3, 4}, {5, 6, 7}, {8, 9, 1}};
saddlePoint(mat);
return 0;
}
```
该代码中用了嵌套循环来遍历每个元素,找到每行的最小值,然后在当前列找到最大值,检查是否存在鞍点。如果存在,则输出其位置和值。如果不存在,则输出找不到鞍点的信息。
阅读全文