首先,判断f(x)是单调递增的。 然后用, double 型的 二分模板
题目来源:
http://acm.hdu.edu.cn/showproblem.php?pid=2199
Can you solve this equation?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7258 Accepted Submission(s): 3362
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.
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 <iostream> #include <algorithm> #include <stdlib.h> #include <stack> #include <iostream> #include <stdio.h> #include <string> #include <string.h> #include <algorithm> #include <stdlib.h> #include <vector> #include <set> #include <math.h> #include <cmath> #include <map> #include <stack> #include <queue> using namespace std ; typedef long long LL ; const double EPS=1e-8; double f(double x) { return 8*pow(x,4)+7*pow(x,3)+2*pow(x,2)+3*x+6; } double Bi_Search(double y) { double left,right,mid; left=0,right=100; while(right-left>EPS) { mid=(left+right)*0.5; if(f(mid)<y) left=mid; else right=mid ; } return (left+right)*0.5; } int main() { int t; cin>>t; while(t--) { double y; cin>>y; if(y>=f(0)&&y<=f(100)) printf("%.4lf\n",Bi_Search(y)); else cout<<"No solution!"<<endl; } return 0; }