Einzelnen Beitrag anzeigen

Popov
(Gast)

n/a Beiträge
 
#12

AW: Lösungsweg für Denkaufgabe

  Alt 11. Feb 2012, 12:43
Einfach geht nicht, trotz Brute Force, denn das kleine Beispiel ist zwar ok, beachtet aber kein Punkt vor Strich. Deshalb führt es nicht zum Erfolg. Letztendlich kommt man um einem Mathparser der die Komplettformel richtig berechnet nicht herum.

Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
  function r(x, y: Double; z: Char): Double;
  begin
    case z of
    '+': Result := x + y;
    '-': Result := x - y;
    '*': Result := x * y;
    '/': Result := x / y;
    else
      Result := x;
    end;
  end;

const
  o = '+-*/';
var
  a, b, c, d: Char;
begin
  repeat
    a := o[Random(4) + 1];
    b := o[Random(4) + 1];
    c := o[Random(4) + 1];
    d := o[Random(4) + 1];
  until r(r(r(r(7, 5, a), 3, b), 8, c), 4, d) = 31;

  ShowMessage(a+b+c+d);
end; //7 5 3 8 4 = 31
  Mit Zitat antworten Zitat