首页
学习
活动
专区
工具
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

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-04-30 02:47:55

首先,你的代码有一个逻辑错误。在pos_max函数中,它将返回列中最大的元素的索引。当列中有多个具有相同值的最大值时,可能会出现这种情况,但是,它返回的不是行中的最小值,因此您的程序将无法打印该鞍点。

要解决这个问题,您可以返回一个包含所有索引的数组,这些索引在一列中最大,然后检查每个点在各自列中是否最小,但我认为这不是一个非常优雅的解决方案。在任何情况下,您都必须为鞍点的其他条件编写完整的代码,最小值在列,最大值在行。

因此,我建议改变策略。您将创建4个数组max_rowmax_colmin_rowmin_col,其中每个数组分别存储该行/列中的最小值/最大值。然后,您可以遍历数组并检查该点是否满足鞍点条件。

代码如下:

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

int a[10][10];
int max_row[10], max_col[10], min_row[10], min_col[10];
int x, y;

bool is_saddle(int i, int j) {
    int x = a[i][j];
    return (max_row[i] == x && min_col[j] == x) || (min_row[i] == x && max_col[j] == x);
}

int main() {
    /* code to input x, y and the matrix
    ...
    */

    /* code to visualize the matrix
    ...
    */

    /* populating max and min arrays */
    for (int i = 0; i <= x-1; ++i) {
        max_row[i] = a[i][0], min_row[i] = a[i][0];
        for (int j = 0; j <= y-1; ++j) {
            max_row[i] = max(max_row[i], a[i][j]);
            min_row[i] = min(min_row[i], a[i][j]);
        }
    }

    for (int j = 0; j <= y-1; ++j) {
        max_col[j] = a[0][j], min_col[j] = a[0][j];
        for (int i = 0; i <= x-1; ++i) {
            max_col[j] = max(max_col[j], a[i][j]);
            min_col[j] = min(min_col[j], a[i][j]);
        }
    }

    /* Check for saddle point */
    for (int i = 0; i <= x-1; ++i) {
        for (int j = 0; j <= y-1; ++j) {
            if (is_saddle(i, j)) {
                cout << "Saddle points are: ";
                cout << "a[" << i + 1 << ", " << j + 1 << "] = " << a[i][j] << "\n";
                flag = 1;
            }
        }
    }

    if (flag == 0) {
        cout << "No saddle points\n";
    }
    cout << "----------------------------\n";
    return 0;
}
票数 0
EN

Stack Overflow用户

发布于 2018-06-09 03:11:36

代码语言:javascript
复制
#include <iostream>
using namespace std;
int getMaxInRow(int[][5], int, int, int);
int getMinInColumn(int[][5], int, int, int);
void getSaddlePointCordinates(int [][5],int ,int );
void getInputOf2dArray(int a[][5], int, int);
int main()
{
    int a[5][5] ;
    int rows, columns;
    cin >> rows >> columns;
    getInputOf2dArray(a, 5, 5);
    getSaddlePointCordinates(a,rows,columns);
}
void getInputOf2dArray(int a[][5], int rows, int columns)
{
    for (int i = 0; i < rows; i = i + 1)
    {
        for (int j = 0; j < columns; j = j + 1)
        {
            cin >> a[i][j];
        }
    }
}
void getSaddlePointCordinates(int a[][5],int rows,int columns)
{
    int flag = 0;
    for (int rowNo = 0; rowNo < 5; rowNo++)
    {
        for (int columnNo = 0; columnNo < 5; columnNo++)
        {
            if (getMaxInRow(a, rows, columns, rowNo) == getMinInColumn(a, rows, columns, columnNo))
            {
                flag = 1;
                cout << rowNo << columnNo;
            }
        }
    }
    if (flag == 0)
        cout << "no saddle point";
    cout << "\n";
}
int getMaxInRow(int a[][5], int row, int column, int rowNo)
{

    int max = a[rowNo][0];
    for (int i = 1; i < column; i = i + 1)
    {

        if (a[rowNo][i] > max)
            max = a[rowNo][i];
    }
    return max;
}
int  getMinInColumn(int a[][5], int row, int column, int columnNo)
{
    int min = a[0][columnNo];
    for (int i = 1; i < row; i = i + 1)
    {
        if (a[i][columnNo] < min)
            min = a[i][columnNo];
    }
    return min;
}
票数 0
EN

Stack Overflow用户

发布于 2019-07-11 21:04:26

只需使用reference arr(refsize) //记忆方法来检查其中的最小值和最大值。

下面是时间复杂度为O(n *n)和空间复杂度为O(N)的代码实现:

代码语言:javascript
复制
#include <bits/stdc++.h>

using namespace std;
#define size 5

void util(int arr[size][size], int *count)
{
    int ref[size]; // array to hold all the max values of row's.

    for(int r = 0; r < size; r++)
    {
        int max_row_val = arr[r][0];
        for(int c = 1; c < size; c++)
        {
            if(max_row_val < arr[r][c])
                max_row_val = arr[r][c];
        }
        ref[r] = max_row_val;
    }

    for(int c = 0; c < size; c++) 
    {
        int min_col_val = arr[0][c];
        for(int r = 1; r < size; r++) // min_val of the column
        {   
            if(min_col_val > arr[r][c])
                min_col_val = arr[r][c];
        }
        for(int r = 0; r < size; r++) // now search if the min_val of col and the ref[r] is same and the position is same, if both matches then print.
        {   
            if(min_col_val == ref[r] && min_col_val == arr[r][c])
            {
                *count += 1;
                if((*count) == 1)
                    cout << "The cordinate's are: \n";
                cout << "(" << r << "," << c << ")" << endl;
            }
        }
    }   
}

// Driver function
int main()
{
    int arr[size][size];
    for(int i = 0; i < size; i++)
    {
        for(int j = 0; j < size; j++)
            cin >> arr[i][j];
    }
    int count = 0;
    util(arr, &count);
    if(!count)
        cout << "No saddle points" << endl;
}


// Test case -> Saddle Point
/*
Input1: 
1 2 3 4 5
6 7 8 9 10
1 2 3 4 5
6 7 8 9 10
0 2 3 4 5
Output1: 
The cordinate's are:
(0,4)
(2,4)
(4,4)
Input2:
1 2 3 4 5
6 7 8 9 1
10 11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
Output2:
No saddle points
*/
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43698407

复制
相关文章

相似问题

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