C language to find the saddle point of a two-dimensional array

Find the "saddle point" of a two-dimensional array with m rows and n columns, that is, the element at this position is the largest on this row and the smallest on this column, where 1<=m,n<=10. There are no identical numbers in the same row and column. Require:

    1) Enter m and n in the format m*n;

    2) Enter m lines with n integers in each line.

    3) Find saddle points.

    4) If a saddle point is found, the row, column and value of the element are output, all in "%d" format (you only need to find the first one).

    5) If the saddle point cannot be found, output None.

Tip: The array length is defined as [10][10], and the elements in the previous m rows and n columns are used according to the input m and n values.

 

Run the reference example:

Enter 1:

3*4

1 2 9 4

5 4 7 2

6 3 8 5

Output 1:

Array[1][2]=7

 

Enter 2:

4*5

1 2 9 4 6

5 4 7 2 8

6 3 8 1 5

3 1 4 5 7

Output 2:

None

The running code is as follows:

#include<stdio.h>
int main()
{
    int a[10][10];
    int i,j,k,t,p,m,n;
    int max,min;
    k=1;t=0;p=0;
    scanf("%d*%d",&m,&n);
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            scanf("%d",&a[i][j]);
        }
            printf("\n");
     } 
    for(i=0;i<m;i++)
    {
        max=a[i][0];//设该行第一个为最大,找出最大值 
        for(j=0;j<n;j++)
        {
            if(max<a[i][j])
            {
                max=a[i][j];
                t=j;//用t记录该行最大的数的下标 (同样是该列的列数) 
            }
        }
            min=a[0][t];
        for(k=0;k<m;k++)
        {
            if(min>a[k][t])//先定义该行最大的数为该列最小的数,看是否为鞍点 
            {
                min=a[k][t];
                p=k;//将最小的行数赋值给p 
            }
        } 
        if(min==max)
        {
            printf("Array[%d][%d]=%d\n",p,t,a[p][t]);
            break;
        }
    }
        if(min!=max)
        {
        printf("None\n");
        }
    return 0;
}

 

 

Guess you like

Origin blog.csdn.net/weixin_74287172/article/details/129912077