HDU - 2899 Strange fuction—三分

HDU - 2899 Strange fuction—三分

题目描述:
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

题目大意:在函数 F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100) ,根据给出的y不同,找到函数取最小值时对应的x的值
思路:因为这个函数在0—100是不单调的,,所以应该进行三分。在区间内找到两个值,m1和m2,(m1

#include <iostream >
#include <algorithm>
#include <cstdio>
#include <cctype>
#include <cmath> 
using namespace std;
double x,y;
double count(double m,double y){
    return 6.0*pow(m,7) + 8.0*pow(m,6) + 7.0*pow(m,3) + 5*m*m - y*m;
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%lf",&y);
        double left = 0,right = 100,m1,m2;
        while((right-left) > 1e-8){
            m1 = (left + right) / 2.0;
            m2 = (m1 + right) / 2.0;
            double f1 = count(m1,y);
            double f2 = count(m2,y);
            if(f1 < f2){
                right = m2;
            }
            else{
                left = m1;
            } 
        }
        printf("%.4lf\n",count(left,y));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Love_Yourself_LQM/article/details/81709441
今日推荐