首页 > 学院 > 开发设计 > 正文

HDU-2899

2019-11-10 18:31:18
字体:
来源:转载
供稿:网友
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.InputThe 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)OutputJust the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.Sample Input
2100200Sample 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));    }}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表