Advertisement
RenSafaray

Untitled

Mar 3rd, 2025
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <limits.h> // Для использования INT_MAX
  3. using namespace std;
  4.  
  5. int main() {
  6.     int N, M;
  7.     cout << "Введите количество строк (N): ";
  8.     cin >> N;
  9.     cout << "Введите количество столбцов (M): ";
  10.     cin >> M;
  11.  
  12.     int A[N][M];
  13.     cout << "Введите элементы массива " << N << "x" << M << ":" << endl;
  14.     for (int i = 0; i < N; i++) {
  15.         for (int j = 0; j < M; j++) {
  16.             cin >> A[i][j];
  17.         }
  18.     }
  19.  
  20.     cout << "Исходный массив:" << endl;
  21.     for (int i = 0; i < N; i++) {
  22.         for (int j = 0; j < M; j++) {
  23.             cout << A[i][j] << " ";
  24.         }
  25.         cout << endl;
  26.     }
  27.  
  28.     int minPositive = INT_MAX;
  29.     bool hasPositive = false;
  30.  
  31.     // Найти минимальный положительный элемент
  32.     for (int i = 0; i < N; i++) {
  33.         for (int j = 0; j < M; j++) {
  34.             if (A[i][j] > 0 && A[i][j] < minPositive) {
  35.                 minPositive = A[i][j];
  36.                 hasPositive = true;
  37.             }
  38.         }
  39.     }
  40.  
  41.     if (!hasPositive) {
  42.         cout << "Положительных элементов в массиве нет." << endl;
  43.         return 0;
  44.     }
  45.  
  46.     bool zeroFound = false;
  47.  
  48.     // Заменить нулевые элементы
  49.     for (int i = 0; i < N; i++) {
  50.         for (int j = 0; j < M; j++) {
  51.             if (A[i][j] == 0) {
  52.                 A[i][j] = minPositive;
  53.                 zeroFound = true;
  54.             }
  55.         }
  56.     }
  57.  
  58.     // Если нулевых элементов не было, заменить первую строку
  59.     if (!zeroFound) {
  60.         for (int j = 0; j < M; j++) {
  61.             A[0][j] = minPositive;
  62.         }
  63.     }
  64.  
  65.     cout << "Преобразованный массив:" << endl;
  66.     for (int i = 0; i < N; i++) {
  67.         for (int j = 0; j < M; j++) {
  68.             cout << A[i][j] << " ";
  69.         }
  70.         cout << endl;
  71.     }
  72.  
  73.     return 0;
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement