Function IsPrime(n:integer):Boolean; var p:integer; found:Boolean; begin found:= (n Mod 2 = 0); p:=3; while (not found) and (sqr(p)<=n) do begin found:=(n Mod p = 0); p:=p+2 end; IsPrime:=(not found) or (p = 2) end;
const n = 12; var a:array[1..n,1..n] of integer; i,j,s,k:integer; begin Randomize; Writeln('Исходный массив'); for i:=1 to n do begin for j:=1 to n do begin a[i,j]:=Random(99)+1; { интервал [1..99] } Write(a[i,j]:3) end; Writeln end; { решаем } s:=0; for i:=1 to n do begin k:=a[i,i]; if IsPrime(k) then a[i,i]:=s; s:=s+a[i,i] end; { выводим } Writeln('Результирующий массив'); for i:=1 to n do begin for j:=1 to n do Write(a[i,j]:5); Writeln end end.
var
p:integer;
found:Boolean;
begin
found:= (n Mod 2 = 0);
p:=3;
while (not found) and (sqr(p)<=n) do
begin
found:=(n Mod p = 0);
p:=p+2
end;
IsPrime:=(not found) or (p = 2)
end;
const
n = 12;
var
a:array[1..n,1..n] of integer;
i,j,s,k:integer;
begin
Randomize;
Writeln('Исходный массив');
for i:=1 to n do
begin
for j:=1 to n do
begin
a[i,j]:=Random(99)+1; { интервал [1..99] }
Write(a[i,j]:3)
end;
Writeln
end;
{ решаем }
s:=0;
for i:=1 to n do
begin
k:=a[i,i];
if IsPrime(k) then a[i,i]:=s;
s:=s+a[i,i]
end;
{ выводим }
Writeln('Результирующий массив');
for i:=1 to n do
begin
for j:=1 to n do Write(a[i,j]:5);
Writeln
end
end.
Тестовое решение:
Исходный массив
55 99 73 7 53 15 30 67 59 59 56 30
81 6 39 10 10 11 60 74 74 48 78 75
24 40 34 44 31 95 75 52 47 66 58 24
87 99 86 92 41 76 44 4 31 57 34 13
31 48 84 80 39 61 86 7 21 97 83 14
80 85 37 63 84 5 82 16 47 16 22 11
24 68 34 97 5 74 45 66 6 22 14 83
12 94 13 86 79 52 4 89 16 73 12 55
40 80 20 27 74 19 65 16 26 74 57 71
28 88 3 46 53 17 8 59 72 54 52 90
91 96 36 95 13 33 94 50 56 78 58 24
55 19 91 86 56 98 57 37 38 42 58 91
Результирующий массив
55 99 73 7 53 15 30 67 59 59 56 30
81 6 39 10 10 11 60 74 74 48 78 75
24 40 34 44 31 95 75 52 47 66 58 24
87 99 86 92 41 76 44 4 31 57 34 13
31 48 84 80 39 61 86 7 21 97 83 14
80 85 37 63 84 226 82 16 47 16 22 11
24 68 34 97 5 74 45 66 6 22 14 83
12 94 13 86 79 52 4 497 16 73 12 55
40 80 20 27 74 19 65 16 26 74 57 71
28 88 3 46 53 17 8 59 72 54 52 90
91 96 36 95 13 33 94 50 56 78 58 24
55 19 91 86 56 98 57 37 38 42 58 91
<head>
<title>Таблица</title>
</head>
<body>
<table border="1" width="600px" height="500px">
<tr>
<td>1 1</td><td width="200px">2 1</td><td>3 1</td><td>4 1</td><td>5 1</td><td width="70px">6 1</td>
</tr>
<tr>
<td>1 2</td><td width="200px">2 2</td><td>3 2</td><td>4 2</td><td>5 2</td><td width="70px">6 2</td>
</tr>
<tr height="50%">
<td>1 3</td><td width="200px">2 3</td><td>3 3</td><td>4 3</td><td>5 3</td><td width="70px">6 3</td>
</tr>
</table>
</body>
</html>