c++找出一个二维数组中的鞍点

任务描述:

找出一个二维数组中的鞍点,即该位置上的元素在该行上最大,在该列最小(也可能没有鞍点)。

测试输入:

1  2  3  4  5

6  7  8  9  10

11 12 13 14 15

16 17 18 19 20

预期输出:

a[0][4]=5

程序源码:

#include <stdio.h>
#include <iostream>
using namespace std;


int main()
{
    
    // 请在此添加代码
    /********** Begin *********/
    int a[4][5];
    int row_max,column_min,temp,row,column,count=1;
    for(int i=0;i<4;i++)
        for(int j=0;j<5;j++)
            cin>>a[i][j];
        

    for(int i=0;i<4;i++)
    {
        row_max=a[i][0];
        row=i;
        column=0;
        for(int j=0;j<5;j++)    //找到行中最大的数
        {
            if(a[i][j] > row_max)    
            {
                row_max=a[i][j];
                row=i;
                column=j;
            }
            
        }

        //找到列中的最小值
        column_min=a[row][column];
        for(int k=0;k<5;k++)
        {
            if(a[k][column] < column_min)     column_min=a[k][column];                
        }

        if(row_max == column_min)    
        {
            count = 0;  //用于判断是否存在鞍点
            printf("a[%d][%d]=%d",row,column,row_max);
        }
    }
         
    if(count)    cout<<"不存在鞍点!";
    
    /********** End **********/
    return 0;
}
原文地址:https://www.cnblogs.com/junfblog/p/12705532.html