
太难了,求攻略,在线等
#include<bits/stdc++.h>
using namespace std;
double f(double x)
{
return x*x*x-5*x*x+16*x-80;
}
int main()
{
double a,b;
cin>>a>>b;
double x=(a*f(b)-b*f(a))/(f(b)-f(a));
while(fabs(f(x))>=1e-6)
{
if(f(x)<0)
a=x;
else
b=x;
x=(a*f(b)-b*f(a))/(f(b)-f(a));
}
cout<<fixed<<setprecision(4)<<x;
return 0;
}

#include <stdio.h>
double func(double x, double b)
{
return x * x * x - 5 * x * x + 16 * x - b;
}
int main(int argc, char const *argv[])
{
double x1 = 0, x2 = 0, x, fx;
double b = 1;
double e = 1e-7;
x = x1;
fx = func(x, b);
printf("fx = %lf\n",fx);
while ((fx > 0 && fx > e) || (fx < 0 && fx < -e))
{
// printf("condition :%d\n",((func(x1, b) > 0) ^ (func(x2, b) > 0)));
if (!((func(x1, b) > 0) ^ (func(x2, b) > 0)))
{
if (func(x1, b) > 0)
{
x1 -= 1;
printf("change x1.\n");
}
else
{
x2 += 1;
printf("change x2.\n");
}
}
else
{
x = (x1 * func(x2, b) - x2 * func(x1, b)) / (func(x2, b) - func(x1, b));
fx = func(x, b);
if (!(fx > 0 ^ func(x1, b) > 0))
x1 = x;
else
x2 = x;
}
printf("temp: %lf,%lf\n", x1, x2);
}
printf("final: %lf,%lf", x, func(x, b));
};
