210693 计算鞍点

题目描述

给定一个5*5的矩阵,每行只有一个最大值,每列只有一个最小值,寻找这个矩阵的鞍点。鞍点指的是矩阵中的一个元素,它是所在行的最大值,并且是所在列的最小值。

例如:在下面的例子中(第4行第1列的元素就是鞍点,值为8 )。

11 3 5 6 9

12 4 7 8 10

10 5 6 9 11

8 6 4 7 2

15 10 11 20 25
输入格式

输入包含一个5行5列的矩阵。

输出格式

如果存在鞍点,输出鞍点所在的行、列及其值,如果不存在,输出"not found"。

样例

样例输入

11 3 5 6 9
12 4 7 8 10
10 5 6 9 11
8  6 4 7 2
15 10 11 20 25
样例输出

4 1 8
数据范围与提示 分类标签

[二维数组]

C++题解代码

#include <bits/stdc++.h>
using namespace std;

int a[6][6];
int b;
int c;
int d;
int e;


// The main procedure
int main() {
  for (int i = 1; i <= 5; i++) {
    for (int j = 1; j <= 5; j++) {
      cin>>a[i][j];
    }
  }
  for (int i = 1; i <= 5; i++) {
    e = i;
    c = 1;
    d = a[i][1];
    for (int j = 2; j <= 5; j++) {
      if (d < a[i][j]) {
        c = j;
        d = a[i][j];
      }
    }
    for (int j = 1; j <= 5; j++) {
      if (d > a[j][c]) {
        e = 0;
        break;
      }
    }
    if (e > 0) {
      break;
    }
  }
  if (e > 0) {
    cout<<e;
    cout<<" ";
    cout<<c;
    cout<<" ";
    cout<<d;
  } else {
    cout<<"not found";
  }
  return 0;
}

Blockly题解代码图片