----Cần Gấp làm bài tập------ Tìm phần tử nguyên dương và là số nguyên tố của mảng. tìm vị trí Pascal

2 câu trả lời

Program BTT;
Uses crt;
Var A:array[1..10000] of longint;
    i,n: longint;

Function ngto(a:longint):boolean;
Var i: longint;
Begin
        If a<2 then exit(false);
        For i:=2 to a div 2 do
                If a mod i = 0 then exit(false);
                                    exit(true);
End;

Begin
        Clrscr;
        Write('Nhap n: '); Readln(n);
        For i:=1 to n do
        Begin
                Write('A[',i,'] = ');
                Readln(A[i]);
        End;

        For i:=1 to n do
                If ngto(A[i]) then
                Begin
                        Write('A[',i,'] = ',A[i]);
                        Writeln;
                End;
        Readln
End.

program bai_giai;
uses crt;
var A:array[1..10000] of integer;
    n,i,d:integer;
function ktnt(k:integer):integer;
var r:integer;
begin
    ktnt:=false;
    if k<2 then exit;
    for r:=2 to trunc(sqrt(k)) do
        if k mod r=0 then exit;
    exit(true);
end;
BEGIN
    clrscr;
    readln(n);
    d:=0;
    for i:=1 to n do
        if ktnt(A[i]) then
            begin
                write(A[i],' ');
                d:=d+1;
            end;
    if d=0 then write('Khong co so duong la nguyen to')
    else
        begin
            write('Xuat hien lan luot tai: ');
            for i:=1 to n do
                if ktnt(A[i]) then write(i,' ');
        end;
    readln;
end.

Câu hỏi trong lớp Xem thêm