Дан одномерный массив. Найти количество пар совпадающих элементов в нем — Pascal(Паскаль)

uses crt;

var
  mas: array [1 .. 20] of integer;
  n, i, j, x, kol: integer;
  // n-размер массива, i,j-счетчики,х-буфер,kol-кол.пар эл-тов

begin
  clrscr;
  write('n=');
  readln(n);
  writeln('Vvedite elem massiva:');
  i := 1;
  while i <= n do
  begin
    write('mas[', i, ']='); // ввод элементов массива
    readln(mas[i]);
    i := i + 1;
  end;
  for i := 1 to n - 1 do // сортировка
    for j := 1 to n - 1 do
      if mas[j] > mas[j + 1] then
      begin
        x := mas[j];
        mas[j] := mas[j + 1];
        mas[j + 1] := x;
      end;
  i := 1;
  kol := 0;
  while i <= n do
    if mas[i] = mas[i + 1] then // подсчет пар (если 1,1,1-то одна пара)
    begin
      kol := kol + 1;
      i := i + 2;
    end
    else
      i := i + 1;
  writeln('kol=', kol);
  readln

end.

Leave a Comment

4 + 4 =