C++求矩阵的鞍点

矩阵的鞍点就是指它在本行中的值最大,在本列中的值最小。

求解思路:

  1. 求出每行的最大值MaxRow以及每列的最小值MinColumn
  2. 保存行最大值的位置和列最小值的位置
  3. 如果行最大值得位置和列最小值的相等则输出此鞍点(如果没有鞍点则输出无)

代码如下:

#include <iostream>
#include<Cmath>
using namespace std;
int main(){
  int n,m;
  cin>>n>>m;
  int MaxRow[1000]={0};    //保存行最大值的行位置 
  int MaxColumn[1000]={0};  //保存行最大值的列位置 
  int MinRow[1000]={0};     //保存列最小值的行位置 
  int MinColumn[1000]={0};  //保存列最小值的列位置
  int RowMax[1000]={0};   //行最大值 
  int ColumnMin[1000]={0};  //列最大值 
  
    int a[n][m];
    for(int i=0;i<n;i++)
    for(int j=0;j<m;j++)
    {
      cin>>a[i][j];
    }
    
  int count=0;
    for(int i=0;i<1000;i++)
      ColumnMin[i]=9999999;

    
   for(int i=0;i<n;i++) {
    for(int j=0;j<m;j++){
       if(a[i][j]>RowMax[i]){
           RowMax[i]=a[i][j];
           MaxRow[i]=i;
           MaxColumn[i]=j;
         }
          if(a[j][i]<ColumnMin[i])
       {
         ColumnMin[i]=a[j][i];
         MinColumn[i]=i;
         MinRow[i]=j;
       }
       }
     }
   for(int i=0;i<n;i++)
   {
    for(int j=0;j<m;j++)
    {
   
    if (MaxRow[i]==MinRow[j]&&MaxColumn[i]==MinColumn[j])
    {
      cout<<"鞍点在第"<<MaxRow[i]+1<<"行,第"<<MaxColumn[i]+1<<"列的"<<a[MaxRow[i]][MaxColumn[i]]<<endl; 
      count++;
    }
   }
 }
   if(count==0)
   {
    cout<<""<<endl;
   }
    
    return 0;
}

 

  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值