Skip to content

Instantly share code, notes, and snippets.

@ramntry
Created November 25, 2011 00:36
Show Gist options
  • Select an option

  • Save ramntry/1392566 to your computer and use it in GitHub Desktop.

Select an option

Save ramntry/1392566 to your computer and use it in GitHub Desktop.
Программа определяет, является ли переданный массив чисел корректной перестановкой
program IsPermutation;
type IntArray = array[1..1024] of integer;
function isPermutation(a: IntArray; size: integer): boolean;
var i, mark: integer;
begin
i := 1;
IsPermutation := true; { Цикл работает до первого нарушения структуры перестановки }
while (IsPermutation = true) and (i <= size) do begin
if a[i] > size then { Число не может превышать размера перестановки }
IsPermutation := false
else begin
mark := abs(a[i]); { Массив чисел используется и для запоминания уже встречавшихся номеров }
if a[mark] > 0 then { Если вхождения данного номера еще не было, }
a[mark] := -a[mark] { ... пометим его и продолжим работу }
else { Если номер уже помечен, }
IsPermutation := false; { ... структура нарушена }
end;
inc(i);
end;
end;
var
a: IntArray;
i: integer;
begin
writeln('Enter a 0-terminated array of integers: ');
i := 0;
repeat
inc(i);
read(a[i])
until a[i] = 0;
if IsPermutation(a, i - 1) then
writeln('is permutation')
else
writeln('is NOT permutation');
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment