pc.bt1

Màu nền
Font chữ
Font size
Chiều cao dòng

Bt1 : Lập CT nhập vào ma trận a[n x n] các ptử là số nguyên.Thực hiện các công việc sau :

a)tính tổng các phần tử là các số nguyên tố trong A.Cho biết các số nguyên tố có mặt trong A.

b)Tính TB cộng của các phần tử chia hết cho 3.

c)Cho biết gtrị lớn nhất cùng các vị trí đạt được giá trị này trong A.

d)XD 1 ma trận b [n x n]từ A sao cho các phần tử của B được xác định :Nếu A[i,j] chẵn thì B[i,j]= 1,ngược lại B[i,j]= 0.

e)cho biết số các phần tử khác nhau trong ma trận A.

program mang2chieu;

var     a,b:array[1..20] of integer;

        n,snt,dnt,sd,dd,max,dongcot:integer;

function nguyento(p:integer):boolean;

var      i:integer;ok:boolean;

begin    i:=2;

         ok:=true;

         repeat

               if p mod i=0 then ok:=false

                            else i:=i+1;

         until not(ok)or(i>p/2);

               if p=2 then ok:=true;

end;

procedure xulynguyento;

var       i,j:integer;

          snt:=0;dnt:=0;

          for i:=1 to n do

          for j:=1 to n do

          if nguyento(a[i,j]) then

          begin snt:=snd+a[i,j];

                dnt:=dnt+1;

          end;

          writeln('tong cac so nguyen to trong A la ',snt);

          writeln(' so nguyen to co mat trong A la ',dnt);

end;

procedure tong duong 3;

var       i,j:integer;

begin

     sd:=0;dd:=0;

     for i:=1 to n do

     for j:=1 to n do

     if (a[i,j]>0) and (a[i,j] mod 3=0 then

     begin

          sd:=sd+a[i,j];

          dd:=dd+1;

     end;

     if dd>0 then

        tbd:=sd,dd;

        else tbd:=0

     writeln('tb cac so duong chia het cho 3 la:',tbd);

end;

procedure xulymax;

var       i,j:integer;

begin

     max:=a[i,j],dong:=0,cot:=0;

     for i:=1 to n do

     for j:=1 to n do

     if max<a[i,j] then max:=a[i,j];

     writeln('gia tri max trong A la ',max);

     for i:=1 to n do

     for j:=1 to n do

     if a[i,j]=max then

     writeln(i,j);

procedure taoB;

var i,j:integer;

begin

     for i:=1 to n do

     for j:=1 to n do

     if (a[i,j] mod 2=0 then b[i,j]:=1

     else b[i,j]:=0;

     writeln('gia tri ma tran b la ');

     for i:=1 to n do

         begin

         for j:=1 to n do

         writeln(b[i,j];

         writeln;

         end;

end;

procedure sophantukhacnhau;

var       i,j:integer;

          c:set of integer;

begin

     c:=[],d:=0;

     for i:=1 to n do

     for j:=1 to n do

     if not(a[i,j]in c) then

     begin

          c:=c+[a[i,j]];

          d:=d+1;

     end;

     writeln('so phan tu khac nhau cua A la ',d);

end;

begin

readln(n);

input;

xulynguyento;

tongduong3;

xulymax;

taoB;

sophantukhacnhau;

end.

Bạn đang đọc truyện trên: Truyen2U.Pro