Matrix75. Дана матрица размера M × N. Элемент матрицы называется ее локальным максимумом, если он больше всех окружающих его элементов. Поменять знак всех локальных максимумов данной матрицы на противоположный. При решении допускается использовать вспомогательную матрицу.
>Решение Pascal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | program Matrix75; type matrix = array [1..10,1..10] of integer; var a:matrix; K, M, N, i, j:Integer; Function CheckMatrixLocMax(var mat:matrix; X,Y:Integer):Boolean; Begin CheckMatrixLocMax:=true; if x>1 then if mat[x,y]<=mat[x-1,y] then CheckMatrixLocMax:=False; if x1 then if mat[x,y]<=mat[x,y-1] then CheckMatrixLocMax:=False; if y1) and (x>1) then if mat[x,y]<=mat[x-1,y-1] then CheckMatrixLocMax:=False; if (y<N) and (x<M) then if mat[x,y]<=mat[x+1,y+1] then CheckMatrixLocMax:=False; if (y>1) and (x<M) then if mat[x,y]<=mat[x+1,y-1] then CheckMatrixLocMax:=False; if (y<N) and (x>1) then if mat[x,y]<=mat[x-1,y+1] then CheckMatrixLocMax:=False; end; begin Write('N: '); Readln(N); Write('M: '); Readln(M); for i:=1 to M do begin writeln(i,': '); for j:=1 to N do begin Write(j,' : '); Read(a[i,j]); end; end; for i:=1 to M do for j:=1 to N do if CheckMatrixLocMax(a,i,j) then a[i,j]:=(-1)*a[i,j]; for i:=1 to M do begin for j:=1 to N do begin Write(' : ',a[i,j]); end; writeln(' : '); end; end. |
Решение C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | # include <iostream> # include <windows.h> # include <cmath> # include <iomanip> # include <fstream> # include "IOMatrix.h" using namespace std; int main () { SetConsoleCP(1251); SetConsoleOutputCP(1251); double matr[RMAX][CMAX]; int nr, nc; input (matr,"Matrix75.txt",nr,nc); output (matr,nr,nc); cout << endl; double matr_reformed[nr][nc]; for (int row = 0; row < nr; ++row) { for (int col = 0; col < nc; ++col) { if((row == 0 || col == 0 || matr[row][col] > matr[row-1][col-1]) && (row == 0 || matr[row][col] > matr[row-1][col]) && (row == 0 || col == nc-1 || matr[row][col] > matr[row-1][col+1]) && (col == 0 || matr[row][col] > matr[row] [col-1]) && (col == nc-1 || matr[row][col] > matr[row] [col+1]) && (row == nr-1 || col == 0 || matr[row][col] > matr[row+1][col-1]) && (row == nr-1 || matr[row][col] > matr[row+1][col]) && (row == nr-1 || col == nc-1 || matr[row][col] > matr[row+1][col+1])) { matr_reformed[row][col]=-matr[row][col]; } else matr_reformed[row][col]=matr[row][col]; } } for (int row = 0; row < nr; ++row) { for (int col = 0; col < nc; ++col){ matr[row][col]=matr_reformed[row][col]; } } output(matr,nr,nc); system ("pause"); return 0; } |
IOMatrix.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | /* Библиотека для работы с матрицами */ # include <iostream> # include <fstream> using namespace std; const int RMAX=100; const int CMAX=100; void input(double a[][CMAX],const char* name, int& nr, int& nc) { ifstream in (name); //cout << "Введите количество строк и столбцов матрицы: "; //cin >> nr >> nc; for (int row = 0; row < nr; ++row) { for (int col = 0; col < nc; ++col) { in >> a[row][col]; } } in.close(); } void output(const double a[][CMAX],int nr, int nc) { for (int row = 0; row < nr; ++row) { for (int col = 0; col < nc; ++col) { cout << '\t' << a[row][col] << " "; } cout << endl; } } void input(int a[][CMAX],const char* name, int& nr, int& nc) { ifstream in (name); //cout << "Введите количество строк и столбцов матрицы: "; //cin >> nr >> nc; for (int row = 0; row < nr; ++row) { for (int col = 0; col < nc; ++col) { in >> a[row][col]; } } in.close(); } void output(const int a[][CMAX],int nr, int nc) { for (int row = 0; row < nr; ++row) { for (int col = 0; col < nc; ++col) { cout << '\t' << a[row][col] << " "; } cout << endl; } } |
Matrix75.txt
1 2 3 4 | 10 3 4 5 6 7 3 9 1 5 7 9 -1 -4 -3 -2 -6 2 6 5 7 -1 4 8 |