Program p31b_4;    {Pag. 57}
{--------------------------------------------------------------
    Se dau doi vectori de numere intregi, 
    fiecare reprezentand o multime.
    La citirea componentelor, algoritmul va urmari ca 
    vectorul sa fie multime.
    
    Sa se determine:
        b) Intersectia multimilor
 --------------------------------------------------------------}

uses CRT;
const
     Max_Elem = 1000;

     {Elementul nr 0 din orice multime va indica nr de elemente din multime}
type
     Elemente = 0..Max_Elem;
     Multime = array[Elemente]of integer;

var
    i, j, ne: Elemente;
    a, b, m: Multime;
    msg: string;

begin
 a[0] := 0;
 Writeln('Introdu nr de elemente din multimea A (0..', max_elem, ')');Readln(ne);
 for i := 1 to ne do begin
  Write(i, ') '); Readln(a[a[0]+1]);
  j := 1; while(j<a[0])and(a[a[0]+1]<>a[j])do inc(j);
  if(a[0] = 0)or(a[a[0]+1]<>a[j])then inc(a[0]);
 end;
 b[0] := 0;
 Writeln('Introdu nr de elemente din multimea B (0..', max_elem, ')');Readln(ne);
 for i := 1 to ne do begin
  Write(i, ') '); Readln(b[b[0]+1]);
  j := 1; while(j<b[0])and(b[b[0]+1]<>b[j])do inc(j);
  if(b[0] = 0)or(b[b[0]+1]<>b[j])then inc(b[0]);
 end;

{b)}
 m[0] := 0;
 for i := 1 to a[0] do for j := 1 to b[0] do if a[i] = b[j] then begin
  inc(m[0]);
  m[m[0]] := a[i];
 end;

  Msg := 'Intersectia A si B: ';
  Write(Msg);
 for i := 1 to m[0] do begin
  if WhereY = hi(windmax)then begin
   Writeln('Apasa o tasta!');
   readkey;
   clrscr;
  end;
  Write(m[i]:8);
 end;
 writeln;

readkey
end.
