题目内容:
给定一个n*n矩阵A。矩阵A的鞍点是一个位置(i,j),在该位置上的元素是第i行上的最大数,第j列上的最小数。一个矩阵A也可能没有鞍点。
你的任务是找出A的鞍点。
输入格式:
输入的第1行是一个正整数n, (1<=n<=100),然后有n行,每一行有n个整数,同一行上两个整数之间有一个或多个空格。
输出格式:
对输入的矩阵,如果找到鞍点,就输出其下标。下标为两个数字,第一个数字是行号,第二个数字是列号,均从0开始计数。
如果找不到,就输出
NO
题目所给的数据保证了不会出现多个鞍点。
#include <stdio.h>
int main()
{
int i,j;
int length,a[101][101];
scanf("%d",&length);
for(i=0;i<length;i++)
for(j=0;j<length;j++)
scanf("%d",&a[i][j]);
int k,mark,count=0;
for(i=0;i<length;i++)
for(j=0;j<length;j++)
{
mark=1; //It's crucial to set a default and roll back after a circle.
for(k=0;k<length;k++)
if(a[i][j]<a[i][k]) mark=0; //find the max number in this cow
if(mark==0) continue;
//Not until it finds the right value, and let it go to the next statement.
for(k=0;k<length;k++)
if(a[i][j]>a[k][j]) mark=0;
if(mark==0) continue;
printf("%d,%d",i,j);
count ++;
}
if(count==0) printf("NO");
return 0;
}