Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.6 kB
3
Indexable
Never
program Bai1;
var a,b,c,d: array [1..30] of integer;
    i,j,n: integer;
    soSNT: integer;
    ketqua: integer;

function kiemtraSNT(s: integer): boolean;
var k: integer;
begin
    kiemtraSNT:=true;
    if s < 2 then kiemtraSNT:=false;
    for k:=2 to trunc(sqrt(s)) do
        if s mod k = 0 then
        begin
            kiemtraSNT:=false;
            break;
        end;
end;

function kiemtraSinhdoi(s1, s2: integer): boolean;
begin
    if (s1-s2 = 2) or (s2-s1 = 2) then
        kiemtraSinhdoi:=true
    else
        kiemtraSinhdoi:=false;
end;

begin
    write('Nhap n:');
    readln(n);
    
    for i:=1 to n do
    begin
        write('A[',i,']=');
        readln(a[i]);
    end;
    
    soSNT:=0;
    for i:=1 to n do
    begin
        if kiemtraSNT(a[i]) = true then
        begin
            soSNT:=soSNT+1;
            b[soSNT]:=a[i];
        end;
    end;

    ketqua:=0;
    for i:=1 to soSNT-1 do
    for j:=i+1 to soSNT do
    begin
        if kiemtraSinhdoi(b[i], b[j]) = true then
        begin
            ketqua:=ketqua+1;
            if b[i] > b[j] then
            begin
                c[ketqua]:=b[i];
                d[ketqua]:=b[j];
            end
            else
            begin
                c[ketqua]:=b[j];
                d[ketqua]:=b[i];
            end;
        end;
    end;
    
    if ketqua > 0 then
    begin
        writeln('Co ',ketqua, ' cap so sinh doi');
        for i:=1 to ketqua do
        begin
            write('(',c[i],' ',d[i],') ');
        end;
    end
    else
        write('Ket qua: khong co cap so nao');
    
    readln;
end.