1 #include<iostream> 2 #include<cmath> 3 #include<iomanip> 4 using namespace std; 5 double a3,a2,a1,a0; 6 double f(double x){ 7 return a3*pow(x,3)+a2*pow(x,2)+a1*x+a0; 8 } 9 int main(){ 10 double a,b,t=0.001; 11 cin>>a3>>a2>>a1>>a0; 12 cin>>a>>b; 13 while(b-a>t){ 14 if(f((a+b)/2)==0){ 15 cout<<setiosflags(ios::fixed)<<setprecision(2)<<(a+b)/2<<endl; 16 break; 17 } 18 else if(f((a+b)/2)*f(a)>0) 19 a=(a+b)/2; 20 else 21 b=(a+b)/2; 22 } 23 if(f((a+b)/2)!=0) 24 cout<<setiosflags(ios::fixed)<<setprecision(2)<<(a+b)/2<<endl; 25 return 0; 26 }
循环-08. 二分法求多项式单根(20),布布扣,bubuko.com
时间: 2024-10-12 22:16:31