// PascalABC.NET 3.1, сборка 1200 от 13.03.2016 function IsPrime(n:integer):boolean; begin if n<4 then Result:=True else begin var found:= (n mod 2 = 0); var p:=3; while (not found) and (sqr(p)<=n) do begin found:=(n mod p = 0); p+=2 end; Result:=not found end end;
begin Writeln('k=',ArrRandom(ReadInteger('n='),1,999).Println. Where(x->IsPrime(x)).Count) end.
// PascalABC.NET 3.1, сборка 1200 от 13.03.2016 procedure IsPrime(n:integer; var res:boolean); begin if n<4 then res:=True else begin var found:= (n mod 2 = 0); var p:=3; while (not found) and (sqr(p)<=n) do begin found:=(n mod p = 0); p+=2 end; res:=not found end end;
begin var a:=ArrRandom(ReadInteger('n='),1,999); a.Println; var k:=0; var prime:boolean; foreach var e in a do begin IsPrime(e,prime); if Prime then Inc(k) end; Writeln('k=',k) end.
Const n=8; var a:array[1..n,1..n] of integer; i,j,k,s,s1,si,dmin,smin:integer; begin Randomize; writeln('Исходный массив:'); for i:=1 to n do begin for j:=1 to n do begin a[i,j]:=random(50); write(a[i,j]:4); end; writeln; end; write('k = '); readln(k); s:=0; for j:=1 to n do s:=s+a[k,j]; writeln('s = ',s); dmin:=999999; smin:=999999; for i:=1 to n do if i<>k then begin s1:=0; for j:=1 to n do s1:=s1+a[i,j]; writeln('s',i,' = ',s1); if abs(s1-s)<dmin then begin dmin:=abs(s1-s); smin:=s1; si:=i; end; end; writeln('Номер строки = ',si,', smin = ',smin); end.
// PascalABC.NET 3.1, сборка 1200 от 13.03.2016
function IsPrime(n:integer):boolean;
begin
if n<4 then Result:=True
else begin
var found:= (n mod 2 = 0);
var p:=3;
while (not found) and (sqr(p)<=n) do
begin
found:=(n mod p = 0);
p+=2
end;
Result:=not found
end
end;
begin
Writeln('k=',ArrRandom(ReadInteger('n='),1,999).Println.
Where(x->IsPrime(x)).Count)
end.
Тестовое решение:
n= 10
401 828 780 444 694 965 23 341 673 875
k=3
2. А вот так это пишется с процедурой
// PascalABC.NET 3.1, сборка 1200 от 13.03.2016
procedure IsPrime(n:integer; var res:boolean);
begin
if n<4 then res:=True
else begin
var found:= (n mod 2 = 0);
var p:=3;
while (not found) and (sqr(p)<=n) do
begin
found:=(n mod p = 0);
p+=2
end;
res:=not found
end
end;
begin
var a:=ArrRandom(ReadInteger('n='),1,999); a.Println;
var k:=0;
var prime:boolean;
foreach var e in a do begin
IsPrime(e,prime);
if Prime then Inc(k)
end;
Writeln('k=',k)
end.
Тестовое решение:
n= 12
199 43 71 365 417 904 170 212 694 103 161 689
k=4
var
a:array[1..n,1..n] of integer;
i,j,k,s,s1,si,dmin,smin:integer;
begin
Randomize;
writeln('Исходный массив:');
for i:=1 to n do
begin
for j:=1 to n do
begin
a[i,j]:=random(50);
write(a[i,j]:4);
end;
writeln;
end;
write('k = '); readln(k);
s:=0;
for j:=1 to n do s:=s+a[k,j];
writeln('s = ',s);
dmin:=999999; smin:=999999;
for i:=1 to n do
if i<>k then
begin
s1:=0;
for j:=1 to n do s1:=s1+a[i,j];
writeln('s',i,' = ',s1);
if abs(s1-s)<dmin then begin dmin:=abs(s1-s); smin:=s1; si:=i; end;
end;
writeln('Номер строки = ',si,', smin = ',smin);
end.
Пример:
Исходный массив:
9 0 22 40 20 35 2 25
23 30 22 35 41 0 9 40
1 15 6 18 43 47 34 33
26 5 2 45 13 46 40 2
26 39 7 31 3 43 20 8
25 15 24 6 10 16 3 25
47 0 27 35 14 15 36 11
16 38 14 14 33 7 11 26
k = 5
s = 177
s1 = 153
s2 = 200
s3 = 197
s4 = 179
s6 = 124
s7 = 185
s8 = 159
Номер строки = 4, smin = 179