Strange fuction


Problem Description

Now, here is a fuction:
  F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)
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
100
200

Sample Output

-74.4291

-178.8534

題意:找出那個函數在x屬於0~100內的最小值。

思路:首先想到的應該是求導求駐點,然後用二分法逼近。

  1. #include "cstdio"  
  2. #include "cstring"  
  3. #include "cmath"  
  4. #define mi 0.00001  
  5. using namespace std;  
  6. double x,y;  
  7. double f(double x)  
  8. {  
  9.     return 42*pow(x,6)+48*pow(x,5)+21*pow(x,2)+10*x-y;  
  10. }  
  11. double fun(double x){  
  12.     return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-x*y;  
  13. }  
  14. int main()  
  15. {  
  16.     int T;  
  17.       
  18.     scanf("%d",&T);  
  19.     while(T--)  
  20.     {  
  21.         scanf("%lf",&y);  
  22.         double l=0,r=100;  
  23.         double min;  
  24.         double mid=(l+r)/2;  
  25.         while(fabs(f(mid))>mi)  
  26.         {  
  27.             mid=(l+r)/2;  
  28.             double a=f(mid);  
  29.             if(a>=0)  
  30.             {  
  31.                 r=mid;  
  32.             }  
  33.             else if(a<0)  
  34.             {  
  35.                 l=mid;  
  36.             }  
  37.         }  
  38.         printf("%.4lf\n",fun(mid));  
  39.     }  
  40.     return 0;  
  41. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章