Matrix74. Дана матрица размера 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 54 | program Matrix74; type matrix = array [1..10,1..10] of integer; var a:matrix; K, M, N, i, j:Integer; Function CheckMatrixLocMin(var mat:matrix; X,Y:Integer):Boolean; Begin CheckMatrixLocMin:=true; if x>1 then if mat[x,y]>=mat[x-1,y] then CheckMatrixLocMin:=False; if x=mat[x+1,y] then CheckMatrixLocMin:=False; if y>1 then if mat[x,y]>=mat[x,y-1] then CheckMatrixLocMin:=False; if y=mat[x,y+1] then CheckMatrixLocMin:=False; if (y>1) and (x>1) then if mat[x,y]>=mat[x-1,y-1] then CheckMatrixLocMin:=False; if (y<N) and (x<M) then if mat[x,y]>=mat[x+1,y+1] then CheckMatrixLocMin:=False; if (y>1) and (x<M) then if mat[x,y]>=mat[x+1,y-1] then CheckMatrixLocMin:=False; if (y<N) and (x>1) then if mat[x,y]>=mat[x-1,y+1] then CheckMatrixLocMin:=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 CheckMatrixLocMin(a,i,j) then a[i,j]:=0; for i:=1 to M do begin for j:=1 to N do begin Write(' : ',a[i,j]); end; writeln(' : '); end; end. |
program matrix74;
type
matrix=array[1..9,1..9] of integer;
var
a,b:matrix;
i,j,m,n:integer;
begin
m:=8;n:=8;
randomize;
for i:=1 to m do begin
for j:=1 to n do begin;
a[i,j]:=random(-50)+10;
end;
end;
b:=a;
writeln();
for i:=1 to m do begin
for j:=1 to n do begin
write(a[i,j],’ ‘);
end;
writeln();
end;
for i:=1 to m do begin
for j:=1 to n do begin
if ( (a[i,j]<a[i,j-1]) and (a[i,j]<a[i,j+1]) and (a[i,j]<a[i-1,j-1]) and (a[i,j]<a[i-1,j]) and (a[i,j]<a[i-1,j+1]) and (a[i,j]<a[i+1,j-1]) and (a[i,j]<a[i+1,j]) and (a[i,j]
end;
end;
writeln();
for i:=1 to m do begin
for j:=1 to n do begin
write(b[i,j],’ ‘);
end;
writeln();
end;
end.
please update above code with this if
if ( (a[i,j]<a[i,j-1]) and (a[i,j]<a[i,j+1]) and (a[i,j]<a[i-1,j-1]) and (a[i,j]<a[i-1,j]) and (a[i,j]<a[i-1,j+1]) and (a[i,j]<a[i+1,j-1]) and (a[i,j]<a[i+1,j])
and (a[i,j]<a[i+1,j+1]) ) then b[i,j]:=0;