您的位置:首页 > 其它

HDU 2899 Strange Fuction(二分查找)

2014-07-21 20:51 417 查看


Strange fuction

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3045 Accepted Submission(s): 2250



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


#include<iostream>

#include<stdio.h>

#include<cmath>

#include<iomanip>

using namespace std;

double y,root,mmin=0;/*如果mmin写为min则会出现编译错误*/

int main()

{

double bs(double,double);

double f(double);

double F(double);

int t;

scanf("%d",&t);

while(t--)

{

//scanf("%llf",&y);

cin>>y;//不能用c语言的格式输入double型数据,不信你可以立即输出y进行检验

if(f(100)<=0){mmin=F(100);}

else if(f(0)>=0){mmin=F(0);}

else{root=bs(0,100);mmin=F(root);}

//printf("%.4lld\n",min);c语言的double输出也有问题

cout<<setiosflags(ios::fixed)<<setprecision(4)<<mmin<<endl;

}

return 0;

}

/*题中原函数是F(x)= 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)*/

/*原函数的导数为f(x)=42*x^6+48*x^5+21*x^2+10*x-y, 导函数在区间[0,100]单调递增*/

double F(double x)

{

return 6*x*x*x*x*x*x*x+8*x*x*x*x*x*x+7*x*x*x+5*x*x-y*x;

}

double f(double x)

{

return 42*x*x*x*x*x*x+ 48*x*x*x*x*x + 21*x*x + 10*x-y;

}

//begin和end是解所在的区间范围

double bs(double begin,double end)

{

double mid;

while(fabs(end-begin)>1e-9)

{

mid=(begin+end)/2;

if(f(mid)>0){end=mid;}

else{begin=mid;}

}

return mid;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: