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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM