Experiment 7-2-8 Find a saddle point (20 points)

The "saddle point" of a matrix element means that the element value at that position is the largest in the row and the smallest in the column.

This question requires writing a program to find the saddle point of a given square matrix of order n.

Input format:
Input the first line to give a positive integer n (1≤n≤6). Next n lines, each line gives n integers, separated by spaces.

Output format:
output the position of the saddle point in a row in the format of "row subscript column subscript" (subscript starts from 0). If the saddle point does not exist, "NONE" is output. The question guarantees that the given matrix has at most one saddle point.

Input example 1:
4
1 7 4 1
4 8 3 6
1 6 1 2
0 7 8 9
Output example 1:
2 1
Input example 2:
2
1 7
4 1
Output example 2:
NONE
title collection complete set portal

#include <stdio.h>

int is_line_max(int n, int x, int i, int a[6][6]);//判断是否为行最大值
int is_rank_min(int n, int x, int j, int a[6][6]);//判断是否为列最小值

int main()
{
    
    
    int n, flag = 0, a[6][6];
    scanf("%d", &n);

    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++)
        for (int j = 0; j < n; j++)
            if (is_line_max(n, a[i][j], i, a))
                if (is_rank_min(n, a[i][j], j, a))
                {
    
    
                    printf("%d %d\n", i, j);
                    flag = 1;    //如果是鞍点,就做标记
                }

    if (flag == 0)
        printf("NONE");
                
    return 0;
}
int is_line_max(int n, int x, int i, int a[6][6])
{
    
    
    for (int k = 0; k < n; k++)
        if (x < a[i][k])
            return 0;

    return 1;
}
int is_rank_min(int n, int x, int j, int a[6][6])
{
    
    
    for (int k = 0; k < n; k++)
        if (x > a[k][j])
            return 0;

    return 1;
}

Guess you like

Origin blog.csdn.net/fjdep/article/details/112709109