• Предмет: Информатика
  • Автор: kdaniluk09
  • Вопрос задан 2 месяца назад

Дана прямокутна цілочисельна матриця. Визначити:

кількість рядків, які не містять жодного нульового елемента;

максимальне із чисел, що зустрічається в заданій матриці більше одного разу.Використовуючи динамічне відділення памяті С++



Треба скинути скрін вивода, Visual Studio

СРОЧЧЧНО

Ответы

Ответ дал: moonflower941
0

#include <iostream>

#include <unordered_map>

#include <vector>

using namespace std;

int rowsWithoutZeros(vector<vector<int>>& matrix) {

   int count = 0;

   for (auto& row : matrix) {

       bool hasZero = false;

       for (int num : row) {

           if (num == 0) {

               hasZero = true;

               break;

           }

       }

       if (!hasZero) {

           count++;

       }

   }

   return count;

}

int maxRepeatedNumber(vector<vector<int>>& matrix) {

   unordered_map<int, int> countMap;

   int maxRepeated = -1;

   for (auto& row : matrix) {

       for (int num : row) {

           countMap[num]++;

           if (countMap[num] > 1 && (maxRepeated == -1 || countMap[num] > countMap[maxRepeated])) {

               maxRepeated = num;

           }

       }

   }

   return maxRepeated;

}

int main() {

   int rows, cols;

   cout << "Enter number of rows and columns: ";

   cin >> rows >> cols;

   vector<vector<int>> matrix(rows, vector<int>(cols));

   cout << "Enter elements of the matrix:" << endl;

   for (int i = 0; i < rows; ++i) {

       for (int j = 0; j < cols; ++j) {

           cin >> matrix[i][j];

       }

   }

   int rowsWithoutZerosCount = rowsWithoutZeros(matrix);

   int maxRepeated = maxRepeatedNumber(matrix);

   cout << "Number of rows without zeros: " << rowsWithoutZerosCount << endl;

   if (maxRepeated != -1) {

       cout << "Max repeated number: " << maxRepeated << endl;

   } else {

       cout << "No number repeated more than once." << endl;

   }

   return 0;

}

Вас заинтересует