您的位置:首页 > 其它

hdu 2899 Strange fuction

2014-08-07 15:51 239 查看
[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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)

[align=left]Output[/align]
Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.

[align=left]Sample Input[/align]

2
100
200


[align=left]Sample Output[/align]

-74.4291
-178.8534----------------------------------#include<stdio.h>
#include<math.h>
double res(double x,double y)
{
return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-y*x;
}
double dao(double x)
{
return 42*pow(x,6)+48*pow(x,5)+21*pow(x,2)+10*x;
}
int main()
{
int t;
double le,ri,mid,y;
scanf("%d",&t);
while(t--)
{
scanf("%lf",&y);
le = 0;
ri = 100;
if(dao(le)-y>=1e-6) // 左边界(dao返回的结果最小)代入后如果导数大于零,说明函数单调递增// 我觉得当函数是开口向下的抛物线时,能
//不能取到最小值,取决于 y 的值
{ printf("%.4f\n",res(le,y)); continue;  }
else if(dao(ri)-y<=1e-6) //右边界(dao返回的结果最大)代入,小于0,说明函数单调递减
{ printf("%.4f\n",res(ri,y)); continue;  }

while(ri-le>1e-6)// 当函数是凹的或者凸的时候,二分搜索直到导数接近于0的时候
{
mid = (le+ri) / 2;
if(dao(mid)-y>=1e-6)// mid处递增,说明最小值在左边
ri = mid;
else  //否则,最小值在右边
le = mid;
}
printf("%.4f\n",(res(le,y)+res(ri,y))/2);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: