鞍点指该位置的元素是该行上最大值,同时也是该列最小值,二维数组可能没有鞍点。
#include <stdio.h>
#define M 4
#define N 3
void fun(int array[M][N]) {
// 循环变量
int i, j;
// 行最大行下标 行最大列下标 列最大行下标
int rowRowIndex, rowColIndex, colRowIndex;
// 行最大值 列最大值
int rowMaxValue, colMinValue;
// 鞍点数量
int num = 0;
for(i=0; i < M; i++) {
rowMaxValue = -10000;
colMinValue = 100000;
// 寻找某行最大值
for(j=0; j < N; j++) {
if(array[i][j] > rowMaxValue) {
rowMaxValue = array[i][j];
rowRowIndex = i;
rowColIndex = j;
}
}
// 寻找行最大值对应下标所在列最小值
for(j=0; j < N; j++) {
if(array[j][rowColIndex] < colMinValue) {
colMinValue = array[j][rowColIndex];
colRowIndex = j;
}
}
// 是鞍点输出
if(rowRowIndex == colRowIndex) {
printf("[%d][%d]=%d为数组鞍点\n", rowRowIndex, rowColIndex, array[rowRowIndex][rowColIndex]);
num++;
}
}
if(num == 0) {
printf("该数组无鞍点\n");
}
}
void main() {
int array[M][N] = {6, 3, 2, 7, 1, 1, 8, 1, 1, 7, 2, 3};
fun(array);
}