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)
Total Submission(s): 15347    Accepted Submission(s): 6829

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!
 
Author
Redow
 
Recommend
lcy
 
题目大意:输入一个数T,接下来输入T组数据(Y),判断是否存在X(0<=X<=100),使得方程8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y成立??
 
思路:二分妥妥的。
注意解出X超过100和小于0不存在,还有精度问题。。。
 
#include <iostream>
#include <math.h>
using namespace std;
double v(double x)
{
    return (8*pow(x,4)+7*pow(x,3)+2*pow(x,2)+3*x+6);
}
double f(double x,double z, double y)
{
double mid;
//int t=50;
while((y-z)>1e-10)//用条件不太好用哈,可以用循环次数t-- { mid=(z+y)/2; if(v(mid)<x) z=mid+1e-10; else y=mid-1e-10; } return mid; } int main() { int n; double a; scanf("%d", &n); while(n--) { scanf("%lf", &a); if(a<6||a>807020306||fabs(f(a,0,100))<1e-4) printf("No solution! "); else printf("%.4lf ", f(a,0,100)); } }
原文地址:https://www.cnblogs.com/Noevon/p/5330191.html