Задача 6 Урок 9

Урок 9 — If

Пользователь вводит три числа, найдите из них максимальное.

Решите тремя способами:

  • С использованием логической операции and.
  • С вложенными блоками (без and).
  • Без вложенных блоков (без and) -- запомнив максимум из двух в специальной переменной.

6.1 С использованием логической операции and.

program maxOf3;
var a,b,c: integer;
begin;
  writeln('what is nums a, b, c ?: ');
  read(a,b,c);
    if ((a>b) and (a>c)) then
       writeln('max of a, b, c is ', a)
    else if ((b>a) and (b>c)) then
       writeln('max of a, b, c is ', b)
    else if ((c>a) and (c>b)) then
       writeln('max of a, b, c is ', c);
    readln();        readln();
end.  

6.2 С вложенными блоками (без and).

program maxOf3;
var a,b,c: integer;
begin;
  writeln('what is nums a, b, c ?: ');
  read(a,b,c);
    if (a>b) then
       begin
       if (a>c) then
          writeln('max of a, b, c is ', a)
       end
    else begin
      if (b>a)then
              if (b>c) then
                 writeln('max of a, b, c is ', b)
      else
         writeln('max of a, b, c is ', c);
      end;
      readln();        readln();
end.

6.3 Без вложенных блоков (без and) -- запомнив максимум из двух в специальной переменной.

program maxOf3;
var a,b,c,x: integer;
begin;
  writeln('what is nums a, b, c ?: ');
  read(a,b,c);
    if (a>b) then
       x:=a
    else if (a<b) then
       x:=b;
    if (x>c) then
          writeln('max of a, b, c is ', x)
    else
          writeln('max of a, b, c is ', c);
      readln();        readln();
end.  
vedro-compota's picture

1) Решение с вложенными операторами:

program maxOf3;
var a,b,c: integer;
begin;
  writeln('what is nums a, b, c ?: ');
  read(a,b,c);
    if (a>b) then
       begin
       if (a>c) then
          writeln('max of a, b, c is ', a)
       end
    else begin
      if (b>a)then
              if (b>c) then
                 writeln('max of a, b, c is ', b)
      else
         writeln('max of a, b, c is ', c);
      end;
      readln();        readln();
end.

не сработает для набора:

5 2 6

2) Проверьте остальные два решения на наборе

5 5 5

что они выведут?

_____________
матфак вгу и остальная классика =)

исправил первый вариант

program maxOf3;
var a,b,c: integer;
begin;
  writeln('what is nums a, b, c ?: ');
  read(a,b,c);
    if ((a>b) and (a>c)) then
       writeln('max of a, b, c is ', a)
    else if ((b>a) and (b>c)) then
       writeln('max of a, b, c is ', b)
    else if ((c>a) and (c>b)) then
       writeln('max of a, b, c is ', c)
    else if ((a=b) and (b=c)) then
       writeln('a, b, c is equal = ', a);
    readln();        readln();
end. 
в наборе 5 2 6 выдаёт 6
в наборе 5 5 5 выдаёт 'a, b, c is equal = 5'

исправил второй вариант

program maxOf3;
var a,b,c: integer;
begin;
  writeln('what is nums a, b, c ?: ');
  read(a,b,c);
    if (a>b) then
       begin
       if (a>c) then
          writeln('max of a, b, c is ', a)
       end
    else begin
      if (b>a)then
              if (b>c) then
                 writeln('max of a, b, c is ', b)
      end;
         if (c>a)then
            if (c>b) then
               writeln('max of a, b, c is ', c);
      if ((a=b) and (b=c)) then
         writeln('a, b, c is equal = ', a);

      readln();        readln();
end. 
в наборе 5 2 6 выдаёт 6
в наборе 5 5 5 выдаёт 'a, b, c is equal = 5'

и третий вариант тоже исправил))

program maxOf3;
var a,b,c,x: integer;
begin;
  writeln('what is nums a, b, c ?: ');
  read(a,b,c);
    if (a>=b) then
       x:=a
    else if (a<=b) then
       x:=b;
    if (x>c) then
         writeln('max of a, b, c is ', x)
    else  if (x=c) then
        writeln('a, b, c is equal = ', x)
    else
         writeln('max of a, b, c is ', c);
      readln();        readln();
end.  
в наборе 5 2 6 выдаёт 6
в наборе 5 5 5 выдаёт 'a, b, c is equal = 5'
vedro-compota's picture

оформить все три решения отдельными темами

_____________
матфак вгу и остальная классика =)