首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >寻找矩阵c++中的所有鞍点

寻找矩阵c++中的所有鞍点
EN

Stack Overflow用户
提问于 2017-04-30 01:13:40
回答 3查看 4.4K关注 0票数 0

我正在编写一个代码,它可以找到矩阵中的所有鞍点。行中的最小和列中的最大,行中的最大和列中的最小都属于(我的大学)鞍点的定义。作为一个初学者,我设法完成了一半的工作(找到马鞍点,它在他们的行中最小,在他们的列中最大),方法是复制我们在课堂上做的部分内容,然后自己输入。我已经被它困住很长一段时间了,不知道如何将行中最大列中最小的鞍点添加到程序中。

这就是我到目前为止所知道的:

代码语言:javascript
复制
#include <iostream>
#include <cstdlib>
using namespace std;

int a[10][10];
int x, y;

int pos_max(int j) //saddle points check
{
    int max = 0;
    for (int i = 1; i <= x - 1; i++) {
        if (a[i][j] > a[max][j]) {
            max = i;
        }
    }
    return max;
}

int main() {
    cout << "Enter the number of rows: ";
    cin >> x;
    cout << "Enter the number of columns: ";
    cin >> y;
    cout << "----------------------------" << endl;

    for (int i = 0; i <= x - 1; i++) //input of the matrix
        for (int j = 0; j <= y - 1; j++) {
            cout << "a[" << i + 1 << ", " << j + 1 << "] = ";
            cin >> a[i][j];
        }
    cout << "----------------------------\n";
    for (int i = 0; i <= x - 1; i++) //visualization of the matrix
    {
        for (int j = 0; j <= y - 1; j++)
            cout << a[i][j] << " ";
        cout << endl;
    }
    cout << "----------------------------\n";

    int r;
    int flag = 0;
    int i = y;

    for (int j = 0; j <= y - 1; j++) {
        r = pos_max(j);
        for (i = 0; i <= y - 1; i++) {
            if (a[r][i] < a[r][j]) {
                break;
            }
        }
        if (i == y) {
            cout << "Saddle points are: ";
            cout << "a[" << r + 1 << ", " << j + 1 << "] = " << a[r][j] << "\n";
            flag = 1;
        }
    }
    if (flag == 0) {
        cout << "No saddle points\n";
    }
    cout << "----------------------------\n";
    return 0;
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43698407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档