Lời giải:
Nhập mảng phải có nhập số lượng phần tử nữa bạn nhé.
program hotrotinhoc;
const fi='bai5.inp';
fo='bai5.out';
var f: text;
i,j,n,max,max1,max2,max3,max4: longint;
a: array[1..32000] of integer;
function dn(x: integer): integer;
var s: integer;
begin
s:=0; dn:=0;
while x<>0 do
begin
s:=s*10+(x mod 10);
x:=x div 10;
end;
dn:=s;
end;
function nt(x1: integer): boolean;
var j: integer;
begin
nt:=false;
if x1<2 then exit;
for j:=2 to trunc(sqrt(x1)) do
if x1 mod j=0 then exit;
nt:=true;
end;
function tongcs(x2: integer): integer;
var s1: integer;
begin
s1:=0;
while x2<>0 do
begin
s1:=s1+(x2 mod 10);
x2:=x2 div 10;
end;
tongcs:=s1;
end;
function ucln(x3,y: integer) : integer;
var z: integer;
begin
while y<>0 do
begin
z:=x3 mod y;
x3:=y;
y:=z;
end;
ucln:=x3;
end;
procedure ip;
begin
assign(f,fi);
reset(f);
readln(f,n);
for i:=1 to n do
read(f,a[i]);
close(f);
end;
procedure out;
begin
assign(f,fo);
rewrite(f);
max:=0; max2:=0; max3:=0;
for i:=1 to n do
begin
if (dn(a[i])=a[i]) and (a[i]>max) then max:=a[i];
if dn(a[i])>max2 then max2:=dn(a[i]);
if (nt(a[i])) and (tongcs(a[i])>max3) then max3:=a[i];
end;
writeln(f,max);
for i:=1 to n do if dn(a[i])=max2 then
writeln(f,a[i]);
max4:=0;
for i:=1 to n do
for j:=i to n do
if (a[i]<>a[j]) and (ucln(a[i],a[j])>max4) then max4:=ucln(a[i],a[j]);
for i:=1 to n do
for j:=i to n do
if ucln(a[i],a[j])=max4 then writeln(f,a[i],' ',a[j]);
write(f,max3);
close(f);
end;
begin
ip;
out;
end.