二分查找Strange fuction

Now, here is a fuction:<br>&nbsp;&nbsp;F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 &lt;= x &lt;=100)<br>Can you find the minimum value when x is between 0 and 100.

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 only one real numbers Y.(0 < Y <1e10)

Output
Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.

Sample Input
   
   
   
   
2<br>100<br>200

Sample Output
   
   
   
   
-74.4291<br>-178.8534
题意就是给你一个方程F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x,求他的最小值,其中0<=x<=100;
最基本的求最小值问题。则求导吧,得 F'(x)= 42 *pow(x, 6 )+ 48 *pow(x, 5 )+ 21 *pow(x, 2 )+ 10 *x-y;
另其为0,则y=42*pow(x,6)+48*pow(x,5)+21*pow(x,2)+10*x;
则就回归到二分查找的问题。
#include<iostream> #include<cmath> #include<stdio.h> using namespace std; double y; double f(double x) {     return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-y*x; } double f1(double x) {     double y = 42*pow(x,6)+48*pow(x,5)+21*pow(x,2)+10*x;     return y; } int main() {     double left=0,right=100,mid,x;     int n;     scanf("%d",&n);     while(n--)     {         left = 0.0;         right = 100.0;     scanf("%lf",&y);     while((right - left) > 1e-8)     {         mid = (left + right)/2;         if(y>f1(mid))         {             left = mid;         }         else             right = mid;     }     printf("%.4lf\n",f(mid));     } }

你可能感兴趣的:(二分查找Strange fuction)