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;
}
相关文章推荐
- HDU 2899 Strange fuction(二分查找根)
- hdu 2899 Strange fuction (二分)
- HDU 2899 Strange fuction (二分)
- HDU 2899 Strange fuction(二分)
- hdu_2899_Strange fuction(三分查找)
- HDU 2899 Strange fuction(二分或三分)
- hdoj 2899 Strange fuction 【二分查找 注意精度】
- hdu 2899 Strange fuction (二分)
- Hdu 2899 - Strange fuction 二分/三分求函数极值点
- HDU - 2899 Strange fuction 二分/三分
- HDU 2899 Strange fuction 二分 + 精度控制
- hdu 2899 Strange fuction(导数+二分)
- HDU 2899 Strange fuction 二分求方程解
- HDU 2899 Strange fuction(二分||三分)
- hdu 2899 Strange Fuction(二分)
- hdu 2899 二分查找
- hdu - 2899 - Strange fuction(二分)
- hdu 2899 Strange fuction 二分
- Hdu 2899 Strange fuction(二分三分可做,模拟退火解法)
- hdu_2899_Strange fuction(三分查找)