1. 程式人生 > >HDU-2199-Can you solve this equation?(二分)

HDU-2199-Can you solve this equation?(二分)

題目連結: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;
Now please try your lucky.

 

 

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
100
-4

Sample Output

1.6152
No solution!

 

題目大意:給出一個方程,然後輸入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!




*/