Advertisement
Caneq

lb3.2.2

Nov 27th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <time.h>
  5. using namespace std;
  6.  
  7. int main() {
  8.  
  9.     setlocale(LC_ALL, "rus");
  10.     int n;
  11.     cout << " Введите порядок матрицы = ";
  12.     cin >> n;
  13.     cout << endl;
  14.     int **a = new int*[n];
  15.     for (int i = 0; i < n; i++)
  16.     {
  17.         a[i] = new int[n];
  18.     }
  19.     int **b = new int*[n];
  20.     for (int i = 0; i < n; i++)
  21.     {
  22.         b[i] = new int[n];
  23.     }
  24.     srand(time(0));
  25.     for (int y = 0; y < n; y++)
  26.     {
  27.         for (int x = 0; x < n; x++)
  28.         {
  29.             a[y][x] = rand() % 100;
  30.         }
  31.     }
  32.     for (int i = 0; i < n; i++)
  33.     {
  34.         for (int j = 0; j < n; j++)
  35.         {
  36.             int x_left, x_right, y_bottom, y_top;
  37.             int maxValue = LONG_MIN;
  38.             x_left = j;
  39.             x_right = j + 2 > n - 1 ? n - 1 : j + 2;
  40.             y_bottom = i;
  41.             y_top = i - 2 < 0 ? 0 : i - 2;
  42.             for (int i = y_bottom; i >= y_top; i--)
  43.             {
  44.                 for (int j = x_right; j >= x_left; j--)
  45.                 {
  46.                         if (maxValue < a[i][j]) maxValue = a[i][j];
  47.                 }
  48.             }
  49.             b[i][j] = maxValue;
  50.         }
  51.     }
  52.     cout << "\t\t" << " Исходная матрица:" << endl << endl;
  53.     for (int y = 0; y < n; y++)
  54.     {
  55.         for (int x = 0; x < n; x++)
  56.         {
  57.             cout << "\t" << a[y][x];
  58.         }
  59.         cout << endl << endl;
  60.     }
  61.     cout << " \t\t" << " Полученнная матрица:" << endl << endl;;
  62.     for (int y = 0; y < n; y++)
  63.     {
  64.         for (int x = 0; x < n; x++)
  65.         {
  66.             cout << "\t" << b[y][x];
  67.         }
  68.         cout << endl << endl;;
  69.     }
  70.     for (int i = 0; i < n; i++)
  71.     {
  72.         delete[] a[i];
  73.         delete[] b[i];
  74.     }
  75.  
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement