CRUELL2 - Cô giáo dạy toán, phần II

Tác giả: ll931110

Ngôn ngữ: Pascal

{$N+}
Program CRUELL2;
  Const
    input  = '';
    output = '';
      maxd = 500;
      maxv = 1000000;
       eps = 0.0001;
  Var
    a: array[0..maxd] of double;
    d: integer;

Procedure init;
  Var
    f: text;
    i: integer;
  Begin
    Assign(f, input);
      Reset(f);

    Readln(f, d);
    For i:= 0 to d do readln(f, a[i]);

    Close(f);
  End;

Function calc(x: double): double;
  Var
    i: integer;
    k: double;
  Begin
    k:= a[d];
    For i:= d - 1 downto 0 do k:= k * x + a[i];
    calc:= k;
  End;

Procedure solve;
  Var
    a,b,c: double;
    ya,yb,yc: double;
    f: text;
  Begin
    Assign(f, output);
      Rewrite(f);

    a:= -maxv;
    b:= maxv;

    While b - a >= eps do
      Begin
        ya:= calc(a);
        If ya = 0 then
          Begin
            Writeln(f, trunc(a * 1000));
            Close(f);
            exit;
          End;

        yb:= calc(b);
        If yb = 0 then
          Begin
            Writeln(f, trunc(b * 1000));
            Close(f);
            exit;
          End;

        c:= (a + b) / 2;
        yc:= calc(c);
        If yc = 0 then
          Begin
            Writeln(f, trunc(c * 1000));
            Close(f);
            exit;
          End;

        If ya * yc < 0 then b:= c else a:= c;
      End;

    c:= (a + b) / 2;
    Writeln(f, trunc(c * 1000));
    Close(f);
  End;

Begin
  init;
  solve;
End.

Download