Title Contents:
Given an n * n matrix A. A saddle point is a matrix position (i, j), the element at that position is the maximum number of the i-th row, the minimum number of j-th column. A matrix may not have a saddle point.
Your task is to find the saddle point A.
Input formats:
The first input line is a positive integer n, (1 <= n <= 100), then there are n rows, each row has n integers, with one or more spaces between two integers on the same row.
Output formats:
Matrix input, if found saddle point, it outputs the index. Subscript two numbers, the first number is the line number and the second number is the row number, from 0 and starts counting.
If not, then output
NO
Title given to ensure that the data multiple saddle point does not appear.
Sample input:
4
1 7 4 1
4 8 3 6
1 6 1 2
0 7 8 9
Sample output:
2 1
#include<stdio.h>
#define nn 100
int main()
{
int n;
scanf("%d",&n);
int max[nn] = {0};//储存每行最大的数
int min[nn] = {0};//储存每列最小的数
int a[nn][nn] = {0};//二维数组
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
scanf("%d",&a[i][j]);
}
}
for(int i=0;i<n;i++){//找到每行最大的数,每列最小的数
int big = 1,small = 100;
for(int j=0;j<n;j++){
if(a[j][i]<small){
small = a[j][i];
}
if(a[i][j]>big){
big = a[i][j];
}
}
max[i] = big;
min[i] = small;
}
int flag = 1;//标志
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(a[i][j]==max[i]&&a[i][j]==min[j]){//做判断是否为鞍点
flag = 0;
printf("%d %d",i,j);
}
}
}
if(flag){//如果没有,则输出NO
printf("NO");
}
}