Editorial for Cô giáo dạy toán, phần II
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này
Code mẫu của happyboy99x
#include<cstdio> #include<cmath> /* F(x) = p[0] + p[1] * x + p[2] * x^2 + ... + p[d] * x^d */ int d; double p[12]; const double EPS = 1e-6; double f(double x) { double res = 0, x2 = 1; for(int i = 0; i <= d; ++i) res += x2 * p[i], x2 *= x; return res; } int main() { scanf("%d", &d); for(int i = 0; i <= d; ++i) scanf("%lf", p+i); double l = -1e6, h = 1e6; if(f(l) > f(h)) for(int i = 0; i <= d; ++i) p[i] = -p[i]; while(fabs(h - l) > EPS) { double x = (l + h) / 2, y = f(x); if(y > 0) h = x; else l = x; } printf("%d\n", int((l+h)/2*1000)); return 0; }
Code mẫu của RR
{$R+,Q+} uses math; const FINP=''; FOUT=''; eps=0.00001; var x,temp,left,right,mid:double; a:array[0..11] of double; n:longint; function cal(x:double):double; var i:longint; kq:double; begin kq:=0; for i:=n downto 0 do kq:=kq*x+a[i]; exit(kq); end; begin assign(input,FINP); reset(input); assign(output,FOUT); rewrite(output); read(n); for n:=0 to n do read(a[n]); left:=-1000000; right:=-left; x:=cal(left); repeat mid:=(left+right)/2; temp:=cal(mid); if abs(temp)<eps then begin writeln(trunc(mid*1000)); exit; end else if temp*x<0 then right:=mid else left:=mid; until left>right; close(input); close(output); end.
Code mẫu của hieult
#include <stdio.h> //#include <conio.h> #include <math.h> long n; double a[13]; double f(double x) { double T=0; for(long i=0;i<=n;i++) T=T+a[i]*pow(x,i); return T; } main() { double u[100],v[100]; long tu=0,tv=0; scanf("%ld",&n); for(long i=0;i<=n;i++) scanf("%lf",&a[i]); if(a[n]>0) { u[0]=-1000000; v[0]=1000000; } else { u[0]=1000000; v[0]=-1000000; } while(v[tv]-u[tu]>0.00001||v[tv]-u[tu]<-0.00001) { //printf("%.0lf ",u[tu]); if(f((v[tv]+u[tu])/2)>0) { tv++; v[tv]=(v[tv-1]+u[tu])/2; } else { tu++; u[tu]=(v[tv]+u[tu-1])/2; } } printf("%ld",long(u[tu]*1000)); //getch(); }
Code mẫu của ll931110
{$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.
Comments