HDU 2199 Can you solve this equation?【二分查找】

解题思路:给出一个方程 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,求方程的解。 首先判断方程是否有解,因为该函数在实数范围内是连续的,所以只需使y的值满足f(0)<=y<=f(100),就一定能找到该方程的解,否则就无解。 然后是求解过程, 假设一个区间[a,b],mid=(a+b)/2,如果f(a)*f(b)<0,那么函数f(x)在区间[a,b]至少存在一个零点,如果f(a)<0,说明0点在其右侧,那么将a的值更新为当前mid的值,如果f(a)>0,说明0点在其左侧,将b的值更新为mid的值。画出图像更好分析。

Can you solve this equation?

Time Limit: 2000/1000 MS (Java/Others)  

  Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9490    Accepted Submission(s): 4382

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!

#include<stdio.h>
#include<string.h>
double f(double x)
{
	return 8*x*x*x*x+7*x*x*x+2*x*x+3*x+6;
}
int main()
{
	int ncase;
	double y,ans,left,right,mid;
	scanf("%d",&ncase);
	while(ncase--)
	{
		scanf("%lf",&y);
		ans=0;
		left=0;
	    right=100;
	    if(f(0)<=y&&y<=f(100))
	    {
		while(right-left>0.000000001)
		{		
			{
				mid=(left+right)*0.5;
				ans=f(mid);
				if(ans-y<0)
				left=mid;
				else
				right=mid;	
			}						
		}
		printf("%.4lf
",mid);
	}
		else
		printf("No solution!
");
	}
}

  

原文地址:https://www.cnblogs.com/wuyuewoniu/p/4181477.html