Pascal ABC. Написать процедуру, которая выводит на экран первые 3 элемента из массива, которые не
равны друг другу. Или процедуру нахождения 3 максимальных элементов массива, которые не равны друг другуОтветы на вопрос
m:array[0..1000] of longint;
n,i:longint;
procedure tf;
var
q,j:longint;
o:array[1..10] of longint;
begin
write(m[1],' ');
q:=1;
o[q]:=m[1];
i:=1;
while q<>3 do
begin
i:=i+1;
if q=1 then
if m[i]<>o[1] then
begin
write(m[i],' ');
q:=q+1;
o[q]:=m[i];
end;
if q=2 then
if (m[i]<>o[1]) and (m[i]<>o[2]) then
begin
write(m[i]);
q:=q+1;
end;
end;
end;
procedure tm;
var
mk:array[1..10] of longint;
begin
for i:=1 to 3 do
mk[i]:=0;
for i:=1 to n do
if mk[1]<m[i] then
begin
mk[3]:=mk[2];
mk[2]:=mk[1];
mk[1]:=m[i];
end else
if (mk[2]<m[i]) and (m[i]<>mk[1]) then
begin
mk[3]:=mk[2];
mk[2]:=m[i];
end else
if (mk[3]<m[i]) and (mk[2]<>m[i]) then mk[3]:=m[i];
write(mk[1],' ',mk[2],' ',mk[3]);
end;
Скорее всего это можно было написать чище и оптимальнее, но вот моё решение на скорую руку
Writing a Procedure in Pascal ABC
To write a procedure in Pascal ABC that outputs the first 3 elements from an array that are not equal to each other, you can follow the steps below:
1. Declare the Procedure: Start by declaring the procedure with the necessary parameters.
```pascal procedure OutputNonEqualElements(arr: array of Integer); ```
2. Initialize Variables: Initialize variables to keep track of the unique elements and the count of non-equal elements.
```pascal var uniqueElements: array[1..3] of Integer; count: Integer; ```
3. Iterate Through the Array: Iterate through the array and store the first 3 non-equal elements in the `uniqueElements` array.
```pascal count := 0; for i := 0 to Length(arr) - 1 do begin if (count < 3) and (not (arr[i] in uniqueElements)) then begin count := count + 1; uniqueElements[count] := arr[i]; end; end; ```
4. Output the Unique Elements: Output the unique elements to the screen.
```pascal for i := 1 to 3 do begin WriteLn(uniqueElements[i]); end; ```
Finding 3 Maximum Non-Equal Elements
To find the 3 maximum non-equal elements in an array, you can use the following procedure:
1. Declare the Procedure: Declare the procedure with the necessary parameters.
```pascal procedure FindMaxNonEqualElements(arr: array of Integer); ```
2. Sort the Array: Sort the array in descending order.
```pascal Sort(arr); ```
3. Find the Maximum Non-Equal Elements: Iterate through the sorted array and find the first 3 non-equal elements.
```pascal var maxElements: array[1..3] of Integer; count: Integer; count := 0; for i := 0 to Length(arr) - 1 do begin if (count < 3) and (not (arr[i] in maxElements)) then begin count := count + 1; maxElements[count] := arr[i]; end; end; ```
4. Output the Maximum Non-Equal Elements: Output the maximum non-equal elements to the screen.
```pascal for i := 1 to 3 do begin WriteLn(maxElements[i]); end; ```
These procedures will help you achieve the desired functionality in Pascal ABC.
Топ вопросов за вчера в категории Информатика
Последние заданные вопросы в категории Информатика
-
Математика
-
Литература
-
Алгебра
-
Русский язык
-
Геометрия
-
Английский язык
-
Химия
-
Физика
-
Биология
-
Другие предметы
-
История
-
Обществознание
-
Окружающий мир
-
География
-
Українська мова
-
Информатика
-
Українська література
-
Қазақ тiлi
-
Экономика
-
Музыка
-
Право
-
Беларуская мова
-
Французский язык
-
Немецкий язык
-
МХК
-
ОБЖ
-
Психология
-
Физкультура и спорт
-
Астрономия
-
Кыргыз тили
-
Оʻzbek tili
