1238:一元三次方程求解
原以为这里的分治体现在分化区间,没想到重心在二分法的分治思想上!┭┮﹏┭┮
1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 5 double a,b,c,d; 6 double fun(double x){ 7 return a*x*x*x+b*x*x+c*x+d; 8 } 9 void print(double v){ 10 printf("%.2lf ",v); 11 } 12 void bin(double b){ 13 double e=b+1,mid; 14 while(e-b>=1e-3){ 15 mid=(b+e)/2; 16 if(fun(b)*fun(mid)<=0)e=mid; 17 else b=mid; 18 } 19 print(mid); 20 } 21 22 int main(){ 23 cin>>a>>b>>c>>d; 24 for(int i=-100;i<100;i++){ 25 if(!fun(i))print(i); 26 if(fun(i)*fun(i+1)<0)bin(i); 27 } 28 return 0; 29 }