Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (* From a list of given numbers, find out which pairs are friends.
- A pair of numbers, (A,B), is friend when A+B is a number which last digit is 0. *)
- program friends_challenge;
- uses crt; // for the Pause method, only
- type TPair = record
- a, b : byte;
- end;
- TList = array of byte;
- TFrs = array of TPair;
- // Pause method (compacted in one line):
- procedure Pause; begin repeat until readkey = #13; end;
- procedure GetNumbers(const q : byte; var list : TList);
- (* Gets the list of numbers from the user. *)
- var i : byte;
- begin
- SetLength(list, q);
- for i:=0 to q-1 do
- readln(list[i]);
- end;
- procedure GetFriends(const list : TList; var frs : TFrs);
- (* Algorithm - gets the list of pairs of numbers which are friends. *)
- const MAX = 255;
- var i, j : byte;
- seen : array [0..MAX, 0..MAX] of boolean;
- begin
- // initialization fo "seen"
- for i:=0 to MAX do for j:=0 to MAX do seen[i,j]:=false;
- // finding the friends...
- for i:=low(list) to high(list)-1 do
- for j:=i+1 to high(list) do
- if ((list[i] + list[j]) mod 10 = 0) and not seen[list[i], list[j]] then begin
- SetLength(frs, Length(frs)+1);
- frs[Length(frs)-1].a := list[i];
- frs[Length(frs)-1].b := list[j];
- seen[list[i], list[j]] := true;
- seen[list[j], list[i]] := true;
- end;
- end;
- var n : byte; // how many numbers will be given
- numbers : TList; // the list of numbers to search
- friends : TFrs; // the list of pairs of number which are friends
- elem : TPair; // usage: FOR-IN cycle
- begin
- // INPUT
- repeat
- write('How many? '); readln(n);
- if (n < 2) then writeln('Must be greater than or equal to 2!');
- until (n >= 2);
- GetNumbers(n, numbers); // getting the list...
- GetFriends(numbers, friends); // getting the friends...
- // OUTPUT
- write('The ', Length(friends), ' friend pairs are: ');
- for elem in friends do write('(', elem.a, ',', elem.b, ') ');
- Pause;
- end.
Advertisement
Add Comment
Please, Sign In to add comment