算法训练——一元三次方程求解
//一元三次方程求解 //直接枚举 #include<stdio.h> #include<math.h> double a,b,c,d,x; double f(double x){ return a*x*x*x + b*x*x + c*x + d; } int main(){ scanf("%lf%lf%lf%lf",&a,&b,&c,&d); for(x=-100;x<=100;++x){ double z = x,y = z+1; if(f(z) == 0){ printf("%.2f ",z); } else{ if((f(z) * f(y)) < 0){ //x位于z与y之间 分治 while(y-z >= 0.001){ double mid = (z+y)/2; if((f(z) * f(mid)) <= 0){ y = mid; } else{ z = mid; } } printf("%.2f ",z); } } } printf("\n"); return 0; }