Strange fuction(二分)

版权声明:转载请注明出处链接 https://blog.csdn.net/qq_43408238/article/details/89738158

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

    这个题目有点意思。题目要求最小值,按照数学步骤,首先求导吧,42*pow(x,6.0)+48*pow(x,5.0)+21*x*x+10*x-y,x的范围是0->100,所以很容易看出红色的部分是递增的(其导函数是递增的),.而0 < Y <1e10,所以极值点就是最值点,只需二分查找42*pow(x,6.0)+48*pow(x,5.0)+21*x*x+10*x=y就可以

#include<iostream>
#include<iomanip>
//#include<bits/stdc++.h>
#include<cstdio>
#include<cmath>
#define PI  3.14159265358979
#define LL long long
#define  eps   0.00000001
using namespace std;
double calcu(double x)//导函数的部分
{
    return (42*pow(x,6.0)+48*pow(x,5.0)+21*x*x+10*x);
}
double cal(double x,double y)//原函数
{
    return  (6*pow(x,7.0)+8*pow(x,6.0)+7*pow(x,3.0)+5*pow(x,2.0)-y*x);
}
int main()
{
    //freopen("input.txt","r",stdin);
    int T;
    cin>>T;double y;
    for(int i=1;i<=T;++i)
    {
        cin>>y;
        double L=0,H=100;double mid;
        while(fabs(H-L)>eps)//二分求极值点
        {
           mid=(L+H)/2;
            if(calcu(mid)>=y) H=mid;
            else L=mid;
        }
        cout<<fixed<<setprecision(4)<<cal(mid,y)<<endl;


    }
}



猜你喜欢

转载自blog.csdn.net/qq_43408238/article/details/89738158