这是一道关于二分法求根的练习题目。
#include <stdio.h>
#include <math.h>
double a,b,c,d;
double sum(double x){
double s;s=a*x*x*x+b*x*x+c*x+d;return s;
}
int main()
{
scanf("%lf %lf %lf %lf",&a,&b,&c,&d);int i;for(i=-100;i<=100;i++){
double left=(double)i,right=(double)(i+1);double mid;if(sum(left)==0) printf("%.2lf ",left);//先判断左端点是否为方程解;else if(sum(left)*sum(right)<0){
//只有在f(a)*f(b)<0是在a与b的区间内才有解;while((right-left)>=0.001){
//精度为0.01//二分法求根的过程mid=(right+left)/2;if(sum(mid)*sum(left)<=0) right=mid;else left=mid;}printf("%.2lf ",left);}}}