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.
2 100 -4
1.6152No solution!
这道题其实跟hud-2899是有相似的,但是更加的简单,在2899里面,有种做法是先求出函数的导数,然后再
去用二分法解决问题,但是这道题就更加的简单,因为根据题目可以看到,这个函数是单调递增的,那么就
可以去解决
#include<iostream> #include<math.h> #include<stdio.h> using namespace std; double cal(double x) { //8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y return (8*pow(x,4) + 7*pow(x,3) + 2*pow(x,2) + 3*x + 6); } int main() { int times; cin>>times; while(times--) { double y_get; cin>>y_get; double low = 0.0; double high = 100.0; double mid = (low+high)/2.0; if(cal(low)==y_get) { printf("%.4f\n",low); continue; } if(cal(high)==y_get) { printf("%.4f\n",high); continue; } if(cal(mid)==y_get) { printf("%.4f\n",mid); continue; } if(cal(0)>y_get||cal(100)<y_get) { cout<<"No solution!"<<endl; continue; } while(fabs(cal(mid)-y_get)>0.00001) { if(cal(mid)>y_get) high = mid; else low = mid; mid = (high+low)/2; } printf("%.4f\n",mid); } }