hdu2199Can you solve this equation?(解方程+二分)

Can you solve this equation?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 25633    Accepted Submission(s): 11018

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值,要求算出x。并且x只能在0~100之间,不然就输出No solution!

题解:因为x只能在0~100之间。所以在知道y值得情况下判断有没有解的方法是:如果给出的Y值比f(0)还小,那他肯定没有0~100之间的解。因为解在0~100之间都是正数。同理也不能大于f(100);

其实上面的可以用这个函数在0~100之间单调递增来解释,比较清楚

剩下的就是二分来找解,看一下代码还是挺容易理解的

 #include<bits/stdc++.h>
using namespace std;
double f(double x)
{
return (*x*x*x*x+*x*x*x+*x*x+*x+);
}
int main()
{ int t;
while(~scanf("%d",&t))
{
while(t--)
{
double y;
scanf("%lf",&y);
if(f()>y||f()<y)
{
printf("No solution!\n");
continue;
}
double l,r;
l=0.0;r=100.0;
double mid=50.0;
while(fabs(f(mid)-y)>1e-)
{
if(f(mid)>y)
{
r=mid;
mid=(l+r)/2.0; }
else
{
l=mid;
mid=(l+r)/2.0;
} }
printf("%.4lf\n",mid);
}
}
return ;
}
上一篇:easyui,datagrid表格,行内可编辑


下一篇:如何用简单例子讲解 Q - learning 的具体过程?