HDU - 2899 Strange fuction

问题描述:

Now, here is a fuction:
F(x) = 6 * x7+8*x6+7x3+5*x2-yx (0 <= x <=100)
Can you find the minimum value when x is between 0 and 100.

输入说明:

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)

输出说明:

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

SAMPLE INPUT:

2
100
200

SAMPLEOUTPUT:

-74.4291
-178.8534

思路:

乍一看和前面一道题目的要求是一样的,但是要注意这里的这个函数在正数范围中并不是单调的,而是会有增有减的,这个时候单纯的用2分就很难做出来了(用导数应该能做出来),所以这里考虑用一个三分去处理,先通过起点和终点,得到一个中点,再根据中点和终点得到一个点,然后比较中点和这个点的函数值的大小,哪一个更靠近最值,就舍去另一个点另一侧的区间。

AC代码:

#include <bits/stdc++.h>
#define E 1e-10
using namespace std;
double y;
int main()
{
    
    
    int t;
    double y;
    double minmum,maxmum,middle1,middle2,temp;
    cin>>t;
    while(t--)
    {
    
    
        cin>>y;
        minmum=0.0;
        maxmum=100.0;
        while(maxmum-minmum>E)
        {
    
    
            middle1=(maxmum+minmum)/2;
            middle2=(middle1+maxmum)/2;
            if((6*pow(middle1,7)+8*pow(middle1,6)+7*pow(middle1,3)+5*pow(middle1,2)-y*middle1)<(6*pow(middle2,7)+8*pow(middle2,6)+7*pow(middle2,3)+5*pow(middle2,2)-y*middle2))
            {
    
    
                maxmum=middle2;
            }
            else
                minmum=middle1;
        }
        cout<<fixed<<setprecision(4)<<(6*pow(middle1,7)+8*pow(middle1,6)+7*pow(middle1,3)+5*pow(middle1,2)-y*middle1)<<endl;
    }
    return 0;
}



猜你喜欢

转载自blog.csdn.net/m0_51727949/article/details/115176264
今日推荐