鞍点指该位置的元素是该行上最大值,同时也是该列最小值,二维数组可能没有鞍点。
#include <stdio.h>
#define M 4
#define N 3void 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);
}