hdu 2199 Can you solve this equation? 简单二分查找

Can you solve this equation?

                                                                         Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
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!
 
  题意:在0~100之间找出一个数满足方程  8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,如果找不到,输出 No solution!。查找的时候注意精度
#include<stdio.h>
double y;
double BinarySearch(double a,double b)  //二分查找
{
	double left=a,right=b,mid;
	while(right-left>1e-10)  //精度
	{
	   mid=(left+right)/2;
	   if(8*mid*mid*mid*mid+7*mid*mid*mid+2*mid*mid+3*mid+6>y)
		   right=mid;
	   else if(8*mid*mid*mid*mid+7*mid*mid*mid+2*mid*mid+3*mid+6<y)
		   left=mid;
	}
 return mid;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%lf",&y);
		if(y<6||y>8*10000*10000+7*10000*100+2*10000+306)  
			printf("No solution!\n");
		else
			printf("%.4lf\n",BinarySearch(0,100));
	}
	return 0;
}
		


你可能感兴趣的:(二分查找)