#include <iostream>
#include <vector>
#include <cstdlib>
#include <climits>
void fill_vector(size_t x,
size_t y,
int range,
std::vector< std::vector <int> > &aVector)
{
for (size_t i = 0; i < x; ++i) {
std::vector <int> temp_vector;
for (size_t j = 0; j < y; ++j) {
temp_vector.push_back(rand() % range);
}
aVector.push_back(temp_vector);
}
}
void print_vector(const std::vector< std::vector <int> > &aVector)
{
for (size_t i = 0; i < aVector.size(); ++i) {
for (size_t j = 0; j < aVector.at(0).size(); j++) {
std::cout << aVector.at(i).at(j) << "\t";
}
std::cout << std::endl;
}
}
bool unique(int aElem,
const std::vector <int> &aVector)
{
int j = 0;
for (size_t i = 0; i < aVector.size(); ++i) {
if (aElem == aVector.at(i)) {
++j;
}
}
return (j > 1) ? false : true;
}
int determine_max_elem(const std::vector< std::vector <int> > &aVector)
{
int max_elem = INT_MIN;
std::vector <int> temp_vector;
for (size_t i = 0; i < aVector.size(); ++i) {
for (size_t j = 0; j < aVector.at(0).size(); j++) {
temp_vector.push_back(aVector.at(i).at(j));
}
}
for (size_t i = 0; i < temp_vector.size(); i++) {
if (max_elem < temp_vector.at(i) &&
unique(temp_vector.at(i), temp_vector)) {
max_elem = temp_vector.at(i);
}
}
return max_elem;
}
int main()
{
std::vector< std::vector <int> > vector;
fill_vector(5, 5, 10, vector); // Fill 2D 5x5 Matrix
print_vector(vector);
int max = determine_max_elem(vector);
if (max != INT_MIN) {
std::cout << "\nMax element is: " << max << std::endl;
} else {
std::cout << "\nMax element in matrix isn't found!" << std::endl;
}
return 0;
}
Решение на C++ в лоб, без применения умных алгоритмов.