Problem Description
给定方程 8x^4 + 7x^3 + 2x^2 + 3x + 6 == Y,请计算x在[0,100]范围内的解。
Input
输入数据首先是一个正整数T(1<=T<=100),表示有T组测试数据。
接下来T行,每行包含一个实数Y ( fabs(Y) <= 1e10 )。
Output
请计算并输出方程在范围[0,100]内的解,结果精确到小数点后4位。
如果无解,则请输出“No solution!”
输入样例
2
100
-4
输出样例
1.6152
No solution!
二分
点击查看代码
#include<bits/stdc++.h>
using namespace std;
double cal(double x)
{
return 8*pow(x,4.0)+7*pow(x,3.0)+2*pow(x,2.0)+3*x+6;//pow(double,double)
}
double b_find(double y)
{
double l=0,r=100;
while(r-l>1e-7)
{
double mid=(l+r)/2;
double res=cal(mid);
if(res>=y) r=mid;
else l=mid;
}
return (r+l)/2;
}
void solve()
{
double y;
cin>>y;
int minn=6,maxn=cal(100);
if(y>maxn||y<minn) cout<<"No solution!"<<'\n';
else cout<<setprecision(4)<<b_find(y)<<'\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout<<setiosflags(ios::fixed);
int T;
cin>>T;
while(T--)
{
solve();
}
return 0;
}
posted on
浙公网安备 33010602011771号