Var a: array[1..n] of integer; k, i, m: byte; min: integer;
begin Write('Razmer massiva: '); Readln(k); if (k < 0) or (k > 100) then begin Writeln('Razmer dolzhen byt bolshe 0 i menshe 100'); Readln; Exit; end; Randomize; Writeln('Ishodn. massiv: '); for i:=1 to k do begin a[i]:=Random(101)-50; write(a[i]:4); end; min:=a[1]; for i:=2 to k do if a[i] < min then begin min:=a[i]; m:=i; end; a[m] := a[1]; a[1] := min; Writeln; Writeln('Min element = ', min:4, ' v posicii ', m:3); Writeln('Rezult. massiv: '); for i:=1 to k do write(a[i]:4); readln; end.
a: array[1..n] of integer;
k, i, m: byte;
min: integer;
begin
Write('Razmer massiva: ');
Readln(k);
if (k < 0) or (k > 100) then
begin
Writeln('Razmer dolzhen byt bolshe 0 i menshe 100');
Readln;
Exit;
end;
Randomize;
Writeln('Ishodn. massiv: ');
for i:=1 to k do
begin
a[i]:=Random(101)-50;
write(a[i]:4);
end;
min:=a[1];
for i:=2 to k do
if a[i] < min then
begin
min:=a[i];
m:=i;
end;
a[m] := a[1];
a[1] := min;
Writeln;
Writeln('Min element = ', min:4, ' v posicii ', m:3);
Writeln('Rezult. massiv: ');
for i:=1 to k do
write(a[i]:4);
readln;
end.
Объяснение:
1. x = 0; y = 4
Цикл по i от 3 до 6
1) i = 3
x = x + 1 = 0 + 1 = 1
y = y + 2 = 4 + 2 = 6
2) i = 4
x = x + 1 = 1 + 1 = 2
y = y + 2 = 6 + 2 = 8
3) i = 5
x = x + 1 = 2 + 1 = 3
y = y + 2 = 8 + 2 = 10
4) i = 6
x = x + 1 = 3 + 1 = 4
y = y + 2 = 10 + 2 = 12
Конец цикла
y = y - x = 12 - 4 = 8
x = x + y = 4 + 8 = 12
Вывод y = 8
2. x = 0; y = 2
Цикл, пока y < 15
1) x = x + 1 = 0 + 1 = 1
y = y + 2 = 2 + 2 = 4 < 15
2) x = 1 + 1 = 2
y = 4 + 2 = 6 < 15
3) x = 2 + 1 = 3
y = 6 + 2 = 8 < 15
4) x = 3 + 1 = 4
y = 8 + 2 = 10 < 15
5) x = 4 + 1 = 5
y = 10 + 2 = 12 < 15
6) x = 5 + 1 = 6
y = 12 + 2 = 14 < 15
7) x = 6 + 1 = 7
y = 14 + 2 = 16 > 15
Конец цикла
y = y - x = 16 - 7 = 9
x = x + y = 7 + 9 = 16
Вывод y = 9
3. x = 0; y = 5
Цикл с постусловием
1) x = x + 1 = 0 + 1 = 1
y = y + 2 = 5 + 2 = 7
Выйти из цикла, если y > 15
y = 7 < 15
2) x = 1 + 1 = 2
y = 7 + 2 = 9 < 15
3) x = 2 + 1 = 3
y = 9 + 2 = 11 < 15
4) x = 3 + 1 = 4
y = 11 + 2 = 13 < 15
5) x = 4 + 1 = 5
y = 13 + 2 = 15
15 не больше 15, поэтому продолжаем.
6) x = 5 + 1 = 6
y = 15 + 2 = 17 > 15
Выход из цикла
y = y - x = 17 - 6 = 11
x = x + y = 6 + 11 = 17
Вывод y = 11