Can you solve this equation?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13001 Accepted Submission(s): 5823
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!
题目与分析:纯粹的二分法求解问题,此题注意精度控制就行了 1e-8 就是科学计数法 先当于 1*10 的 -8 次方也就是0.00000001
其他应该都不是问题了,都是最基本的二分法 没一点扩展
#include<cstdio> #include<cstring> #include<iostream> using namespace std; double f(double v) { return 8*v*v*v*v + 7*v*v*v+ 2*v*v + 3*v + 6; } double fabs(double v) { return v>=0?v:-v; } int main() { int n; cin>>n; while(n--) { double y; cin>>y; if(y<6||y>f(100)) printf("No solution!\n"); else { double left=0.0,right=100.0,mid=0.0; while(fabs(right-left)>1e-8) { mid=(left+right)/2.0; if(f(mid)<y) left=mid; else right=mid; } printf("%.4lf\n",mid); } } return 0; }