2<br>100<br>-4<br>
1.6152<br>No solution!<br>
题目大意:
一个多项式,给出y值,让你求x的值。
思路:
将题目看作f(x) = 8*x^47*x^3 2*x^2 3*x 6, x∈[0, 100]时,可以发现整个函数都是递增的,用二分法即可解题
感想:
其实就是一道中学代数题,因为有思路,所以不难,具体就是二分法来做,有引文x<-[0,100]是递增的,所以有规律
#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
double f(double x)
{
return (8 * pow(x, 4) + 7 * pow(x, 3) + 2 * pow(x, 2) + 3 * x + 6);
}
using namespace std;
int main()
{
int t;
cin >> t;
for (int i = 0;i<t;i++)
{
double y, mid;
cin >> y;
if (f(0)>y || f(100)<y)cout << "No solution!" << endl;
else
{
double left = 0, right = 100;
while (right - left>1e-10)
{
mid = (right + left) / 2;
if (f(mid)>y)
right = mid;
else left = mid;
}
printf("%.4f\n", mid);
}
}
return 0;
}