1. 程式人生 > >HDU 2899 Strange fuction 二分

HDU 2899 Strange fuction 二分

cnblogs clu return abs print names sca 求導 range

1.題意:給一個函數F(X)的表達式,求其最值,自變量定義域為0到100

2.分析:寫出題面函數的導函數的表達式,二分求導函數的零點,對應的就是極值點

3.代碼:

 1 # include <iostream>
 2 # include <cstdio>
 3 # include <cmath>
 4 using namespace std;
 5 const double eps=1e-8;
 6 double Y;
 7 int sgn(double x)
 8 {
 9     if(fabs(x)<eps) return 0;
10     if(x>0
) return 1; 11 else return -1; 12 } 13 double F(double x) 14 { 15 return 6.0*pow(x,7)+8.0*pow(x,6)+7.0*pow(x,3)+5.0*pow(x,2)-Y*x; 16 } 17 double f(double x) 18 { 19 return 42.0*pow(x,6)+48.0*pow(x,5)+21.0*pow(x,2)+10.0*x-Y; 20 } 21 void Solve() 22 { 23 scanf("%lf",&Y); 24 double l=0; 25
double r=100; 26 while(r-l>eps) 27 { 28 double mid=l+(r-l)/2.0; 29 if(sgn(f(mid))>0) r=mid; 30 else l=mid; 31 } 32 printf("%.4f\n",F(l)); 33 } 34 int main() 35 { 36 int T; 37 scanf("%d",&T); 38 while(T--) 39 { 40 Solve(); 41 }
42 return 0; 43 }

HDU 2899 Strange fuction 二分