HDU-2899

来源:互联网 发布:六壬排盘软件安卓 编辑:程序博客网 时间:2024/06/09 04:27
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
2100200
Sample Output
-74.4291

-178.8534

这道题在做的时候,最开始觉得这个函数应该是用三分法解决,但是用了之后,会发现怎么都ac不了,都是

wa,然后在网上查完资料之后,发现这道题就应该是先求导,之后再根据导数的正负,来运用二分法解决问

题,因为我们要求的是一个最值而跟极值的概念还是有不同的,之后将过程实现,就可以ac了,这道题感觉

就是可以运用数学知识解决问题

#include<iostream>#include<cmath>#include<cstdio>using namespace std;double cal(double x,double y){    //6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)    return(6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-y*x);}double cal_d(double x,double y){    return(42*pow(x,6)+48*pow(x,5)+21*pow(x,2)+10*x-y);}int main(){    int times;    cin>>times;    while(times--)    {        long long y_get;        cin>>y_get;        double low = 0,high = 100;        double mid;        while(low<high-0.000001)        {            mid = (low+high)/2;            if(cal_d(mid,y_get)>0) high = mid;            else low = mid;        }        printf("%.4f\n",cal(mid,y_get));    }}


0 0
原创粉丝点击