5-30 找鞍點 (20分)

來源:互聯網
上載者:User

5-30 找鞍點   (20分)

一個矩陣元素的“鞍點”是指該位置上的元素值在該行上最大、在該列上最小。

本題要求編寫程式,求一個給定的nn階方陣的鞍點。 輸入格式:

輸入第一行給出一個正整數nn(1\le n\le 61≤n≤6)。隨後nn行,每行給出nn個整數,其間以空格分隔。 輸出格式:

輸出在一行中按照“行下標 列下標”(下標從0開始)的格式輸出鞍點的位置。如果鞍點不存在,則輸出“NONE”。題目保證給出的矩陣至多存在一個鞍點。 輸入範例1:

41 7 4 14 8 3 61 6 1 20 7 8 9
輸出範例1:
2 1
輸入範例2:
21 74 1
輸出範例2:
NONE

#include <cstdio>#include <cstdlib>#include <algorithm>using namespace std;int main(){    int n,flag = 1;    int row[6],col[6],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]);            row[i] = j==0?a[i][j]:max(row[i],a[i][j]);            col[j] = i==0?a[i][j]:min(col[j],a[i][j]);        }    }    for(int i = 0;i < n && flag;i++){    for(int j = 0;j < n;j++){    if(a[i][j] == row[i] && a[i][j] == col[j]){    printf("%d %d\n",i,j);    flag = 0;    break;}}    }    if(flag) printf("NONE\n");    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.