马鞍点是所在行的行最小值,所在列的列最大值。
  程序思路是:逐行求解每一行的最小值,并判断该数组元素是否是所在列的最大值,若是鞍点,则输出到屏幕。

#include<iostream>
using namespace std;
#include<stdio.h>
#define RANK 4
#define COLUMN 4

void findSaddlePoint(int array[RANK][COLUMN]) {
	int min,minColumn;
	int rank, column;
	bool find;
	for (rank = 0; rank < RANK; rank++) {
		find = true;  //suppose we can find such saddle in this rank
		min = array[rank][0];
		for(column = 1 ; column < COLUMN ; column++)
			if (min > array[rank][column]) {
				min = array[rank][column];
				minColumn = column;
			}  //has found the min element in this rank
		
		for (int i = 0; i < RANK; i++) 
			if (i == rank)
				continue;		//pass  the saddle point itself
			else if(array[i][minColumn] >= array[rank][minColumn]){
				find = false;
				break;
			}
		
		if (find) 
			cout << "saddle point : array[" << rank<< "][" 
				<< minColumn << "] : " << min<< endl;
	}
}
int main() {
	int array[RANK][COLUMN] = { {9,7,6,8},
								{20,26,22,25},
								{28,36,25,30},
								{12,4,2,6} };
	for (int i = 0; i < RANK; i++) {
		for (int j = 0; j < COLUMN; j++)
			printf("%5d",array[i][j]);
		cout << endl << endl;
	}

	findSaddlePoint(array);
	return 0;
}

在这里插入图片描述
结果和课本里是一样的。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐