首页 > 代码库 > 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.
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。