Actually you need to find the largest element in the matrix, which occurs> 1 time. Please help with the algorithm.

    3 answers 3

    #include <iostream> #include <iomanip> #include <cstdlib> #include <set> using namespace std; int main() { int matrix[10][10]; srand(time(0)); for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { matrix[i][j] = rand() % 50; // заполняем } } set <int> myset; set <int>::iterator it; bool dupFound = false; int maxValue; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { int v = matrix[i][j]; if (dupFound && v <= maxValue) continue; it = myset.find(v); if(it == myset.end()) myset.insert(v); else { maxValue = v; if(!dupFound) dupFound = true; } } } for (int i = 0; i < 10; i++){ for (int j = 0; j < 10; j++) cout << setw(2) << matrix[i][j] << ' '; cout << endl; } cout << maxValue << endl; return 0; } 
       #define SIZE 100 int array[SIZE]; bool have_such_number(int idx) { int n = array[idx]; for( ++idx; idx < SIZE; ++idx ) { if( array[idx] == n ) return true; } return false; } int get_duplicated_maximum(void) { int max = INT_MIN; for( int i = 0; i < SIZE; ++i ) { if( array[i] > max && have_such_number(i) ) { max = array[i]; } } return max; } 
         #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; int main(){ int matrix[10][10]; srand(time(0)); for (int i = 0; i < 10; i++){ for (int j = 0; j < 10; j++){ matrix[i][j] = rand() % 50; // заполняем } } int max = 0; for (int i = 0; i < 10; i++){ for (int j = 0; j < 10; j++){ if (matrix[i][j] > max) max = matrix[i][j]; // находим наибольшее } } int count = 0; for (int i = 0; i < 10; i++){ for (int j = 0; j < 10; j++){ if (matrix[i][j] == max) count++; // подсчитываем } } for (int i = 0; i < 10; i++){ for (int j = 0; j < 10; j++){ cout << matrix[i][j] << "_"; // выводим } } cout << endl << endl; if (count > 1){ cout << "max digit:" << max << " count:" << count << endl; // выводим } return 0; } 

        -------------------------- rewritten later

         #include <iostream> #include <stdlib.h> #include <time.h> #include <vector> using namespace std; int main(){ int matrix[10][10]; vector<int> v; srand(time(0)); for (int i = 0; i < 10; i++){ for (int j = 0; j < 10; j++){ matrix[i][j] = rand() % 50; // заполняем } } int max = 0; for (int i = 0; i < 10; i++){ for (int j = 0; j < 10; j++){ for (int a = i + 1; a < 10; a++){ for (int b = j + 1; b < 10; b++){ if (matrix[i][j] == matrix[a][b]) v.push_back(matrix[i][j]); } } } } for (int i = 0; i < 10; i++){ for (int j = 0; j < 10; j++){ cout << matrix[i][j] << "_"; // выводим матрицу } } cout << endl << "_______________________" << endl; for (int i = 0; i < v.size(); i++){ cout << v[i] << "_"; // выводим вектор } for (int i = 0; i < v.size(); i++){ for (int j = 0; j < v.size(); j++){ if ((v[i] > v[j]) && (v[i] > max)){ max = v[i]; // находим наибольшее } } } cout << endl << "max:" << max << endl; // ответ return 0; } 

        ------------------- added later

         #include <iostream> #include <stdlib.h> #include <time.h> #include <vector> using namespace std; int main(){ int matrix[10][10]; // = { 2, 2, 1, 1, 1, 1, 1, 1, 1 }; vector<int> v; srand(time(0)); for (int i = 0; i < 10; i++){ for (int j = 0; j < 10; j++){ matrix[i][j] = rand() % 50; // заполняем } } int max = 0;//, a = 0; for (int i = 0; i < 10; i++){ for (int j = 0; j < 10; j++){ for (int a = i; a < 10; a++){ for (int b = j + 1; b < 10; b++){ if (matrix[i][j] == matrix[a][b]){ v.push_back(matrix[i][j]); //break; } } //a++; } } } for (int i = 0; i < 10; i++){ for (int j = 0; j < 10; j++){ cout << matrix[i][j] << "_"; } } cout << endl << "_______________________" << endl; for (int i = 0; i < v.size(); i++){ cout << v[i] << "_"; } if (v.size() != 0){ max = v[0]; } for (int i = 0; i < v.size(); i++){ for (int j = 0; j < v.size(); j++){ if ((v[i] > v[j]) && (v[i] > max)){ max = v[i]; } } } if (v.size() != 0){ cout << endl << "max:" << max << endl; } return 0; } 
        • In your case, it turns out that we are checking the largest element of the matrix itself. And if it will be like this: 1 2 3 0 2 6 In this case, the correct answer is 2, not 6. Because 6 occurs once, although the largest element in the matrix. - Vladyslav B.
        • really. Sorry did not understand the essence of the question, rewrote. sloppy truth a bit - perfect
        • Use map, where the key is the value from the matrix, and the map value is the number of entries. - alexlz
        • 2
          @perfect, but did you try to test the program (already rewritten)? -- one). What happens if all elements are negative? 2). Imagine the matrix 2 2 1 ... 1 1 1 .... 1 ..... 1 1 ..... 1 i. the first two (in the zero line) are twos, and all the rest are one. IMHO you in v they will not fall. Your idea is clear, put all the repeating elements in v , but its implementation is lame. 3) Why continue to look for repetitions (and put them in v ) after the first one found? - IMHO the simplest algorithm is to copy the matrix, sort it and find the first repeat. - avp
        • units get and do not need two more units (looking for the maximum), and if there are negative numbers then the max variable is initialized to -65535 (if the type is int). I ran this code 10 times like it works. said right away that the curve code is a bit. with checking the code for stopping the loop as soon as the repetition was found was not due to the complication of the code, and the consumption of cycles for checking also increases - perfect