Спираль из центра массива по часовой стрелке (для нечетных n): const n=7; var a:array[1..n,1..n] of integer; i,j,d,k,l,m:integer; begin k:=n*n; i:=n div 2 + 1; j:=n div 2 + 1; a[i,j]:=k; d:=1; for m:=1 to n div 2 do begin for l:=1 to d do begin k:=k-1; j:=j+1; a[i,j]:=k; end; for l:=1 to d do begin k:=k-1; i:=i+1; a[i,j]:=k; end; d:=d+1; for l:=1 to d do begin k:=k-1; j:=j-1; a[i,j]:=k; end; for l:=1 to d do begin k:=k-1; i:=i-1; a[i,j]:=k; end; d:=d+1; end; d:=d-1; for l:=1 to d do begin k:=k-1; j:=j+1; a[i,j]:=k; end; for i:=1 to n do begin for j:=1 to n do write(a[i,j]:4); writeln; end; end.
//PascalABC.NET 3.1 сборка 1200 begin var n := ReadInteger('n='); var a := MatrixRandom(n, n, 0, 10); println(a);
var d := new System.Collections.Generic.Dictionary<integer, integer>(); for var i := a.GetLowerBound(0) to a.GetUpperBound(0) do for var j := a.GetLowerBound(1) to a.GetUpperBound(1) do begin if d.ContainsKey(a[i, j]) then d[a[i, j]] += 1 else d.Add(a[i, j], 1); end;
var p := d.OrderByDescending(x -> x.Value).First(); println('Чаще всего встречается ' + p.Key + ', ' + p.Value + ' раз'); end.
const n=7;
var a:array[1..n,1..n] of integer;
i,j,d,k,l,m:integer;
begin
k:=n*n;
i:=n div 2 + 1; j:=n div 2 + 1;
a[i,j]:=k;
d:=1;
for m:=1 to n div 2 do
begin
for l:=1 to d do
begin k:=k-1; j:=j+1; a[i,j]:=k; end;
for l:=1 to d do
begin k:=k-1; i:=i+1; a[i,j]:=k; end;
d:=d+1;
for l:=1 to d do
begin k:=k-1; j:=j-1; a[i,j]:=k; end;
for l:=1 to d do
begin k:=k-1; i:=i-1; a[i,j]:=k; end;
d:=d+1;
end;
d:=d-1;
for l:=1 to d do
begin k:=k-1; j:=j+1; a[i,j]:=k; end;
for i:=1 to n do
begin
for j:=1 to n do write(a[i,j]:4);
writeln;
end;
end.
Результат:
7 6 5 4 3 2 1
8 29 28 27 26 25 24
9 30 43 42 41 40 23
10 31 44 49 48 39 22
11 32 45 46 47 38 21
12 33 34 35 36 37 20
13 14 15 16 17 18 19
begin
var n := ReadInteger('n=');
var a := MatrixRandom(n, n, 0, 10);
println(a);
var d := new System.Collections.Generic.Dictionary<integer, integer>();
for var i := a.GetLowerBound(0) to a.GetUpperBound(0) do
for var j := a.GetLowerBound(1) to a.GetUpperBound(1) do
begin
if d.ContainsKey(a[i, j]) then
d[a[i, j]] += 1
else
d.Add(a[i, j], 1);
end;
var p := d.OrderByDescending(x -> x.Value).First();
println('Чаще всего встречается ' + p.Key + ', ' + p.Value + ' раз');
end.