Задание 18 Урок 13.1
Primary tabs
Задание 18 Урок 13.1
Модифицируйте решение предыдущей задачи, так, чтобы длина возрастающего фрагмента каждый раз увеличивалась на единицу (начиная с двух):
ПРИМЕЧАНИЕ: эту задачу можно решить, как вложенными циклами, так и вообще одним циклом (что более изящно), при этом решение одним циклом можно сделать, как используя делимость нацело (для определения момента вывода тройки), так и не используя.
Решите всеми тремя способами.
Решение вложенным циклом
var counter : integer; output_3 : integer; n : integer; bottom_number : integer; label m1; begin bottom_number := 0; output_3 := 1; counter := 8; m1 : write('Введите число N и нажмите ENTER: '); readln(n); if (n <= 0) then writeln('ОШИБКА!!! число отрицательное'); goto m1; else while (counter <= n) do begin repeat write(counter, ' '); counter += 2; bottom_number += 1; until (bottom_number > output_3); write(3, ' '); output_3 += 1; bottom_number := 0; end; readln(); end.
Решение делением нацело
var counter : integer; output_3 : integer; n : integer; bottom_number : integer; label m1; begin output_3 := 2; bottom_number := 0; m1 : write('Введите число N и нажмите ENTER: '); readln(n); if (n <= 0) then begin writeln('ОШИБКА!!! число отрицательное'); goto m1; end else for counter := 8 to n do begin if ((counter mod 2) = 0) then begin bottom_number += 1; write(counter, ' '); end; if (bottom_number = output_3) then begin write(3, ' '); output_3 += 1; bottom_number := 0; end; end; readln(); end.
Решение без деления нацело
var counter : integer; output_3 : integer; n : integer; bottom_number : integer; label m1; begin output_3 := 2; bottom_number := 0; counter := 8; m1 : write('Введите число N и нажмите ENTER: '); readln(n); if (n <= 0) then begin writeln('ОШИБКА!!! число отрицательное'); goto m1; end else repeat write(counter, ' '); counter += 2; bottom_number += 1; if (bottom_number = output_3) then begin write(3, ' '); output_3 += 1; bottom_number := 0; end; until (counter > n); readln(); end.
- Log in to post comments
- 616 reads