题目:Can you solve this equation?
连接:杭电HNUST专题联练习 1000
思路:对所给函数直接求一阶导和二阶导可知,函数在0~100是单调递增。所以可直接用二分法求解
#include <stdio.h>
#include <math.h>
int main()
{
int T;
double x1, x2, n, mid,ans1,ans2, ans;
scanf("%d", &T);
while(T--)
{
x1 = 0;
x2 = 100;
scanf("%lf", &n);
ans1 = 8 * pow(x1, 4) + 7 * pow(x1, 3) + 2 * pow(x1, 2) + 3 * x1 + 6 - n;
ans2 = 8 * pow(x2, 4) + 7 * pow(x2, 3) + 2 * pow(x2, 2) + 3 * x2 + 6 - n;
if (ans1 > 0 || ans2 < 0) //如果ans1大于零或者ans2小于零,证明无解
printf("No solution!\n");
else
{
while (fabs(x1 - x2) >= 1e-8) //限定范围,使得值更精确
{
mid= (x1 + x2) / 2;
ans = 8 * pow(mid, 4) + 7 * pow(mid, 3) + 2 * pow(mid, 2) + 3 * mid + 6 - n;
if (ans >= 0)
x2 = mid;
else
x1 = mid;
}
printf("%0.4f\n", mid);
}
}
return 0;
}