第1个回答 2011-03-15
#include <iostream>
#include <cmath>
using namespace std;
double f(double x)
{
return x*(x*(x-3)+6)-1;
}
int main()
{
double x0, x1=0, x2=1, f0, f1, f2, er = 0.001;
f1 = f(x1);
f2 = f(x2);
while ( x2 - x1 > er)
{
x0 = ( x1 + x2) / 2;
f0 = f(x0);
if( f0 * f1 < 0)
{
x2 = x0;
f2 = f0;
}
else
{
x1 = x0;
f1 = f0;
}
}
cout<<"root = "<<(x1+x2)/2<<endl;
return 0;
}