Data structure machine three: Application of multidimensional arrays-looking for custom "saddle points" (minimum value per row and maximum value per column)

Computer 3: Application of multi-dimensional array

Content requirements

Find a custom "saddle point" in the two-dimensional array, that is, the minimum value of each row and the maximum value of each column, and output;
if it does not exist, output "the two-dimensional array has no saddle point"

Code

#include<iostream>//3:多维数组的应用
using namespace std;
int a[10][10];
void in(int n)
{
	int i,j;
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			cin>>a[i][j];
		}
	}
}
int find(int n)//鞍点:每行最小值且每列最大值(自定义鞍点)
{
	int i,j,flag,flag1,k,www=1;
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			flag=0;
			flag1=0;
			for(k=0;k<n;k++)
			{
				if(a[i][j]>a[i][k])//在当前行中是否为最小,不是最小flag=1;
					flag=1;
			}
			for(k=0;k<n;k++)
			{
				if(a[i][j]<a[k][j])//当前列是否为最大
					flag1=1;
			}
			if(!flag&&!flag1)//flag=flag1=0
			{
				cout<<i+1<<"行"<<j+1<<"列"<<a[i][j]<<endl;
				www=0;
				return 1;	
			}
		}
	}
	if(www==1)//没有鞍点
	{
		return 0;
	}
}
int main()
{
	int n;
	cout<<"建立n*n的二维数组,请输入n值(1<n<10):";cin>>n;
	cout<<endl;
	cout<<"输入数组元素:"<<endl;
	in(n);
	int m=find(n);
	if(m==0)
		cout<<"该二维数组没有鞍点"<<endl;
}

Readme

The gold content of this article is not high, so don't praise it 23333

Guess you like

Origin blog.csdn.net/qq_43704702/article/details/104029467