首页 > 代码库 > hdu 2899

hdu 2899

 

Strange fuction

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3209    Accepted Submission(s): 2348


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
2100200
 


 

Sample Output
-74.4291-178.8534
 


 

Author
Redow

 

代码如下:

<span style="font-size:14px;">#include<stdio.h>#include<stdlib.h>#include<math.h>#define eps 1e-6 double cal(double x){    return 42*pow(x,6)+48*pow(x,5)+21*pow(x,2)+10*pow(x,1);}//自己设立求导函数 double sum(double x,double y){    return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-x*y;}int main(){    int n;    scanf("%d",&n);    while(n--)    {        double low=0,high=100,y,mid;        scanf("%lf",&y);        while(high-low>eps)        {//因为是double型所以比较的时候,要考虑精度的问题             mid=(high+low)/2;            if(cal(mid)<y)//这是三分法,呵呵呵 导数小于零,继续因为是凹形             low=mid+1e-8;            else            high=mid-1e-8;        }        printf("%0.4lf\n",sum(mid,y));    }    return 0;}</span>


该链接有关于二分法,三分法的一点小小的体会,不了解的可以去看看

http://blog.csdn.net/ice_alone/article/details/38420043