HDU 2899 Strange fuction [二分]

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
",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 }
原文地址:https://www.cnblogs.com/cnXuYang/p/7218820.html