Time Limit : 1 秒
Memory Limit : 128 MB
找出具有m行n列二维数组Array的“鞍点”,即该位置上的元素在该行上最大,在该列上最小,其中1<=m,n<=10。同一行和同一列没有相同的数。
Input
输入数据有多行,第一行有两个数m和n,下面有m行,每行有n个数。
Output
按下列格式输出鞍点:
Array[i][j]=x
其中,x代表鞍点,i和j为鞍点所在的数组行和列下标,我们规定数组下标从0开始。
一个二维数组并不一定存在鞍点,此时请输出None。
我们保证不会出现两个鞍点的情况,比如:
3 3
1 2 3
1 2 3
3 6 8
Examples
input
复制
3 3 1 2 3 4 5 6 7 8 9
output
复制
Array[0][2]=3
#include<iostream>
#include<math.h>
#include<algorithm>
#include<vector>
#include<ctype.h>
#include<cmath>
#include<sstream>
#include<string>
#define PI 3.1415927
using namespace std;
const int N=1e6+1;
int main()
{ std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int n,m;
cin>>n>>m;
int nums[n][m];
//输入二维数组的值
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>nums[i][j];
}
}
bool found = false;
//这里判断时候能够找到鞍点
for(int i=0;i<n;i++)
{
//找到每一行中的最大值,确定它的列索引
int colIdx=0;
for(int j=0;j<m;j++)
{
if(nums[i][j]>nums[i][colIdx])
{
colIdx=j;
}
}
//注意:这里是在最外面的循环的内部的
//所以还是每一的进行判断
bool IsMINInCol = true;
//判断是否为列中的最小值
for(int k=0;k<m;k++)
{
if(nums[k][colIdx]<nums[i][colIdx])
{
IsMINInCol = false;
break;
}
}
if(IsMINInCol)
{
cout << "Array[" << i << "][" << colIdx << "]=" << nums[i][colIdx] << endl;
found = true;
break;
}
}
if(!found)
{
printf("None");
}
}
代码可以AC