Skip to content

Instantly share code, notes, and snippets.

@henrybear327
Created November 6, 2015 13:52
Show Gist options
  • Save henrybear327/6403f2d538fc3cb3b29b to your computer and use it in GitHub Desktop.
Save henrybear327/6403f2d538fc3cb3b29b to your computer and use it in GitHub Desktop.
root.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define N 1010
#define ERROR 0.0000001
double f(int n,double x,double c[]) {
double fx,xi;
int i;
fx=c[0];xi=1;
for (i=1;i<=n;i++) {
xi*=x;
fx+=c[i]*xi;
}
return fx;
}
int main()
{
int i , j , n,ncase,k;
double co[10];
double left,right,mid,f1,f2,f3;
freopen("root.in","r",stdin);
scanf("%d",&ncase);
while (ncase--) {
scanf("%d",&n);//printf("%d %d:",n,k);
for (i=n;i>=0;i--) scanf("%lf",co+i);
scanf("%lf %lf",&left,&right);
f1=f(n,left,co);
f2=f(n,right,co);
if (f1*f2>0) {printf("NO\n");continue;}
while (right-left>ERROR) {
mid=(left+right)/2;
f3=f(n,mid,co);
if (f1*f3>0) {left=mid;f1=f3;}
else {right=mid;f2=f3;}
}
//printf("%.7lf, %.7lf, %lf %lf\n",left,right,f1,f2);
printf("%.5lf\n",left);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment