申请一个n;行m;列数列的马龙数,输出它的位置。所谓马龙数,是指在行上唯一的最小而在列上唯一的最大的数。如下:

n=5 m=5 5 6 7 8 9 4 5 6 7 8 3 4 5 2 1 2 3 4 9 0 1 2 5 4 8

则1;行1;列上的数就是马龙数。

输出: 共n+1;行,第一行:nm(n,m<10) 第2;到n+1;行:每行m;个数字(每个数都<10)

输出: 输出许多行:如果存在马龙数,则输出所有马龙数,每行一个,为行和列以及马龙数。 如果不存在马龙数,则输出“not exit”;。 用C++内容:

#include&lt;iostream&gt;
using namespace std;

int main(){
    int n, m;
    cin &gt;&gt; n &gt;&gt; m;
    int matrix[n][m];
    for(int i=0; i&lt;n; i++){
        for(int j=0; j&lt;m; j++){
            cin &gt;&gt; matrix[i][j];
        }
    }
    
    bool flag = false;
    for(int i=0; i&lt;n; i++){
        int min_row = matrix[i][0];
        int col_index = 0;
        for(int j=1; j&lt;m; j++){
            if(matrix[i][j] &lt; min_row){
                min_row = matrix[i][j];
                col_index = j;
            }
        }
        
        int max_col = matrix[0][col_index];
        for(int k=1; k&lt;n; k++){
            if(matrix[k][col_index] &gt; max_col){
                max_col = matrix[k][col_index];
            }
        }
        
        if(min_row == max_col){
            cout &lt;&lt; "Row: " &lt;&lt; i+1 &lt;&lt; ", Column: " &lt;&lt; col_index+1 &lt;&lt; ", Value: " &lt;&lt; min_row &lt;&lt; endl;
            flag = true;
        }
    }
    
    if(!flag){
        cout &lt;&lt; "not exit" &lt;&lt; endl;
    }
    
    return 0;
}

原文地址: https://www.cveoy.top/t/topic/p5nC 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录