05: saddle point calculation

Total time limit: 1000ms memory limit: 65536kB
description
given a 5 * 5 matrix, and each row has only one maximum value, a minimum value for each column only find the saddle point matrix.
It refers to a saddle point of the element of the matrix, which is the maximum value of the row, and the column is the minimum value is located.
For example: In the following example (saddle point element is the one in the fourth row, the value 8).
. 5. 6. 9. 3. 11
12 is 10. 4. 7. 8
10. 5. 11. 9. 6
. 8. 4. 7. 6 2
15 10 20 is 25. 11

Input
Input contains a 5 row matrix 5 the
output
line if there is a saddle point, the output saddle point where the column and their values, if not, outputs "not found"
sample input
. 11. 3 5. 6. 9
12 is. 4. 7. 8 10
10 5 . 9. 11. 6
. 8. 4. 7. 6 2
15 10 20 is 25. 11
sample output
418

#include<iostream>
using namespace std;
int main(){
	int a[10][10];
	for(int row=1;row<=5;row++)                                //输入5*5个元素 
	for(int col=1;col<=5;col++){
		cin>>a[row][col];
	}
	
	
	int max[6],maxCol[6];                                      //max[i]表示第i行最大的元素值,maxCol[i]表示该元素所在的列
	for(int row=1;row<=5;row++){
		max[row]=a[row][1];
		maxCol[row]=1;
		for(int col=1;col<=5;col++)
		if(a[row][col]>max[row]){
			max[row]=a[row][col];
			maxCol[row]=col;
		}
	}
	
	
	                                                          //判断一行最大的元素max[row]是否为最大元素所在列maxCol[row]的最小元素
	for(int row=1;row<=5;row++){
		int flag=1;                                           //flag用来判断 
		for(int i=1;i<=5;i++){
			if(a[i][maxCol[row]]<max[row])
			flag=0;
		}
		if(flag==1){                                          // flag为0 表示元素所在的列有小于“行最大元素”的情况,表示其不再是该列的最小值 ,也就不符合题目行最大列最小的要求 
		                                                      // flag==1 表示该行row的最大值确实为该列maxCol[row]的最小值 
			cout<<row<<' '<<maxCol[row]<<' '<<a[row][maxCol[row]];
			return 0;
		}
	}
	
	cout<<"not found";
	
	
	
	return 0;
}
Published 36 original articles · won praise 0 · Views 354

Guess you like

Origin blog.csdn.net/weixin_44437496/article/details/103997126