题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2199
Problem Description Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can you find its solution between 0 and 100;
Input The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has a real number Y (fabs(Y) <= 1e10);
Output For each test case, you should just output one real number(accurate up to 4 decimal places),which is the solution of the equation,or “No solution!”,if there is no solution for the equation between 0 and 100.
Sample Input 2 Sample Output 1.6152
|
题目大意:给出一个方程,然后输入y,对于每个输入的y,输出精确到小数点后4为的解(四舍五入的答案),如果没有解,输出"No solution!"
题目分析:首先这个函数是一个单调递增的函数,因此直接二分0~100,然后注意精度,输出的时候取后四位即可
是二分总结中的 3.查找最后一个与key相等的元素的位置
ac:
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<map>
//#include<set>
#include<deque>
#include<queue>
#include<stack>
#include<bitset>
#include<string>
#include<fstream>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b)
#define clean(a,b) memset(a,b,sizeof(a))// 水印
//std::ios::sync_with_stdio(false);
const int MAXN=5e4+10;
const int INF=0x3f3f3f3f;
const ll mod=1e9+7;
const double PI=acos(-1.0);
double y;
int judge(double m)
{
//mid在答案的左边
return y-(8*m*m*m*m+7*m*m*m+2*m*m+3*m+6)>1e-16;
}
int main()
{
//std::ios::sync_with_stdio(false);
int T;
while(~scanf("%d",&T))
{
while(T--)
{
scanf("%lf",&y);
if(y>807020306||y<6)
{
cout<<"No solution!"<<endl;
continue;
}
//找最后一个等于key的mid
double l=0,r=100,mid;
while(l<r-1e-8)
{
mid=(l+r)/2;
//cout<<"l,r,mid:"<<l<<" "<<r<<" "<<mid<<endl;
if(judge(mid))
l=mid;
else//mid>=key r
r=mid;
}
printf("%.4lf\n",r);
}
}
}
/*
Sample Input
2
100
-4
Sample Output
1.6152
No solution!
*/