题意:求出方程:
8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y的解,通过输入Y找到x的值。
解题思路:本题 类型:二分;可通过二分法不断缩小x的范围从而确定x的值。
感想:简单的二分题目,不断判断x范围,可套用经典的二分题目模板完成。
代码:
#include<cmath>
#include<iomanip>
#include<cstdio>
#include<iostream>
using namespace std;
double f(double x)
{
return 8*x*x*x*x+7*x*x*x+2*x*x+3*x+6;
}
int main()
{
int t;double y,mid;
cin>>t;
while(t--)
{
cin>>y;
if(y<f(0)||y>f(100))
{
cout<<"No solution!"<<endl;
continue;
}
double left=0.0,right=100.0;
while(right-left>1e-8)
{
mid=(right+left)/2.0;
if(abs(abs(f(mid))-y)<1e-6) break;
if(f(mid)<y)
left=mid;
else right=mid;
}
cout<<fixed<<setprecision(4)<<mid<<endl;
}
return 0;
}