lab3.cpp

来自「若在m×n的矩阵中有一个元素a[i,j]满足下述条件:a[i,j]既是第i行元素」· C++ 代码 · 共 55 行

CPP
55
字号
#include <iostream>
#define MAX 10
using namespace std;

int main() {
	int array[MAX][MAX];//array to store the matrix
	int size;			//size of the array
	int rowMaxValue;	//use to store the local max value
	int colMinValue[MAX];	//use to store the local min val.
	int rowMax[MAX];	//use to store the pos of max.
	int colMin[MAX];	//use to store the pos of min.
	int i;
	int j;
	
	srand(time(0));	//initialize
	cout << "input the size length of the array :\n";
	cin >> size;
	//construct the array to store the matrix
	cout << "The size of the array is [ " << size << " ]\n";	
	for(i = 0; i < size; i++) {
		for(j = 0; j < size; j++) {
			array[i][j] = rand() % 100;
			cout << array[i][j] << "\t";
		}
		cout << "\n";
		rowMax[i] = 0;
		colMin[i] = 100;
		colMinValue[i] = 100;
	}
	//scan the matrix to find the local maxs and mins
	for(i = 0; i < size; i++) {	//row i
		rowMaxValue = 0;
		for(j = 0; j < size; j++) {	//col j
			if(rowMaxValue < array[i][j]) {
				rowMaxValue = array[i][j];
				rowMax[i] = j;
			}
			if(colMinValue[j] > array[i][j]) {
				colMinValue[j] = array[i][j];
				colMin[j] = i;
			}
		}
	}
	//search for the required point.
	for(i = 0; i < size; i++) {
		if(colMin[rowMax[i]] == i) {
			cout << "Found, The point is " << array[i][rowMax[i]] << ",\nat row " << i << " and column " << rowMax[i] << "\n";
			return 1;//found
		}
	}
	cout << "Not Found!";//not found
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?