Anonymous - 1 year ago
398 0

No description

Pascal

# UE0502

``````VAR
a3size : INTEGER;

(* returns TRUE if the array contains the value *)
FUNCTION Contains (arr : ARRAY OF INTEGER; value : INTEGER): BOOLEAN;
VAR
i : INTEGER;
ret : BOOLEAN;
BEGIN
ret := FALSE;
FOR i := 0 TO Length(arr)-1 DO BEGIN
IF arr[i] = value THEN BEGIN
ret := TRUE;
i := Length(arr);
END;
END;
Contains := ret;
END;

(* set every number with the given value to -1 (both arrays) *)
PROCEDURE Disable (VAR a1, a2 : ARRAY OF INTEGER; value : INTEGER);
VAR
i : INTEGER;
BEGIN
FOR i:=0 TO Length(a1)-1 DO BEGIN
IF a1[i] = value THEN BEGIN
a1[i] := -1;
END
ELSE IF a1[i] > value THEN i := Length(a1);
END;
FOR i:=0 TO Length(a2)-1 DO BEGIN
IF a2[i] = value THEN BEGIN
a2[i] := -1;
END
ELSE IF a2[i] > value THEN i := Length(a2);
END;
END;

(* append the value to the end of the array *)
PROCEDURE AppendToArr (VAR a3 : ARRAY OF INTEGER; value : INTEGER);
BEGIN
a3size := a3size + 1;
a3[a3size-1] := value;
END;

(* merge the two arrays into the third, ignore same values *)
PROCEDURE Merge (a1, a2 : ARRAY OF INTEGER; VAR a3 : ARRAY OF INTEGER; VAR n3 : INTEGER);
VAR
i : INTEGER;
smaller : INTEGER;
BEGIN
FOR i := 0 TO Length(a1)-1 DO BEGIN
IF Contains(a2, a1[i]) AND (a1[i] <> -1) THEN BEGIN
Disable(a1, a2, a1[i]);
END;
END;

IF Length(a1) > Length(a2) THEN BEGIN smaller := Length(a2); END
ELSE BEGIN smaller := Length(a1); END;

FOR i := 0 TO smaller-1 DO BEGIN
IF a1[i] <> -1 THEN BEGIN AppendToArr(a3, a1[i]); END;
IF a2[i] <> -1 THEN BEGIN AppendToArr(a3, a2[i]); END;
END;

IF Length(a1) > Length(a2) THEN BEGIN
FOR i := smaller TO Length(a1)-1 DO
IF a1[i] <> -1 THEN BEGIN AppendToArr(a3, a1[i]); END;
END ELSE BEGIN
FOR i := smaller TO Length(a2)-1 DO
IF a2[i] <> -1 THEN BEGIN AppendToArr(a3, a2[i]); END;
END;
n3 := a3size;
END;

(* main logic *)
VAR
a1, a2, a3 : ARRAY OF INTEGER;
n3, i : INTEGER;
BEGIN
SetLength(a1, 6);
a1[0] := 2;
a1[1] := 4;
a1[2] := 4;
a1[3] := 10;
a1[4] := 15;
a1[5] := 15;

SetLength(a2, 4);
a2[0] := 3;
a2[1] := 4;
a2[2] := 5;
a2[3] := 10;

SetLength(a3, Length(a1) + Length(a2));
a3size := 0;

Merge(a1, a2, a3, n3);

FOR i := 0 TO a3size-1 DO WriteLn(a3[i]);

END.``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download