本关任务:在矩阵中,一个元素在所在行中是最大值,在所在列中是最小值,则被称为鞍点(Saddle point)。求所给矩阵的鞍点,给出C语言代码
时间: 2024-06-06 09:10:40 浏览: 86
#include <stdio.h>
#define ROW 3
#define COL 4
int main()
{
int matrix[ROW][COL] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}};
int i, j, max, min, saddle_point_flag;
for (i = 0; i < ROW; i++) {
max = matrix[i][0];
for (j = 1; j < COL; j++) {
if (matrix[i][j] > max) {
max = matrix[i][j];
}
}
for (j = 0; j < COL; j++) {
min = matrix[0][j];
for (int k = 1; k < ROW; k++) {
if (matrix[k][j] < min) {
min = matrix[k][j];
}
}
if (max == min) {
saddle_point_flag = 1;
printf("Saddle point at (%d,%d): %d\n", i, j, max);
}
}
}
if (!saddle_point_flag) {
printf("No saddle point.\n");
}
return 0;
}
阅读全文