Chương trình:
uses crt;
var f1, f2: text;
a: array[1..500] of longint;
i,n,z,dema,demc,demd: integer; tongb: longint; tongc: real;
lasnt: boolean;
begin
{Khai bao bien ghi file}
assign(f1, 'dl.pas');
reset(f1);
assign(f2, 'kq.pas');
rewrite(f2);
{Doc du lieu}
readln(f1,n);
for i:=1 to n do
readln(f1,a[i]);
{Cau a}
for i:=1 to n do
if ((a[i] mod 2 = 1) and (a[i] mod 7 = 0)) then dema:=dema+1;
{Cau b}
for i:=1 to n do
if ((a[i]>=10) and (a[i]<=30)) then
tongb:=tongb+a[i];
{Cau c}
for i:=1 to n do
if ((frac(a[i]/2) = -0.5) and (a[i] < 0)) then
begin
tongc:=tongc+a[i];
demc:=demc+1;
end;
if (demc<>0) then tongc:=tongc/demc else tongc:=0;
{Cau d}
for i:=1 to n do
begin
if ((a[i] <> 0) and (a[i] <> 1)) then
begin
for z:=2 to a[i]-1 do
begin
if (a[i] mod z = 0) then lasnt:=false;
end;
end
else lasnt:= false;
if (lasnt=true) then
begin
demd:=demd+1;
lasnt:=true;
end
else lasnt:=true;
end;
{Ghi ket qua: }
writeln(f2, 'Cau a: So luong phan tu le chia het cho 7 la: ',dema);
writeln(f2, 'Cau b: Tong cac so trong day co gia tri tu 10 den 30 la: ',tongb);
writeln(f2, 'Cau c: Trung binh cong cac so trong day co gia tri am la: ',tongc:0:0);
writeln(f2, 'Cau d: So luong so nguyen to: ',demd);
close(f1);
close(f2);
end.
====================
Dữ liệu vào (DL.pas)
15
7
21
14
18
40
26
-4
-7
2
3
5
7
12
15
13
----------------------
Đầu ra:
Cau a: So luong phan tu le chia het cho 7 la: 3
Cau b: Tong cac so trong day co gia tri tu 10 den 30 la: 119
Cau c: Trung binh cong cac so trong day co gia tri am la: -7
Cau d: So luong so nguyen to: 7