原题链接:
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!
题目大意:题目大意就是给你一个函数
Now, here is a fuction:
F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100) ,然后给你F(X)的数值。计算在0-100区间内的解,如果解不在0-100区间内,那么输出No solution!
F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100) ,然后给你F(X)的数值。计算在0-100区间内的解,如果解不在0-100区间内,那么输出No solution!
思路:二分,第一次尝试二分法,查了很多人的资料,初步理解了二分法。
代码:
#include<stdio.h> #include<math.h> double f(double x) { return 8*pow(x,4.0)+7*pow(x,3.0)+2*pow(x,2.0)+3*x+6; } //函数求导可以发现,在0-100是单调递增的 int main() { double max=f(100); //最大值 double min=f(0); //最小值 int T; double Y,result,l,r; scanf("%d",&T); while(T--) { scanf("%lf",&Y); if(Y>max||Y<min) //不在区间的判定 { printf("No solution!\n"); continue; } else { l=0.0;r=100.0; while(r-l>1e-10) { result=f((l+r)/2); //二分法 if(result<Y) { l=((l+r)/2); } else r=((l+r)/2); } printf("%.4lf\n",(l+r)/2); } } return 0; }