转载亲请注明出处:http://blog.csdn.net/u012860063/article/details/21318849
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2899
----------------------------------------------------------------------------------------------------------------------------------------------------------
欢迎光临天资小屋:http://user.qzone.qq.com/593830943/main
----------------------------------------------------------------------------------------------------------------------------------------------------------
首先简介一下三分思想:
大家都知道二分搜索算法作为分治中最常见的方法,适用于单调函数,逼近求解某点的值。但当函数具有凹凸性时,二分搜索就无法使用了(当然可以先排序),这里介绍一种新的方法,三分查找算法,适应的函数图形如下:
mid = (left + right) / 2;
midmid = (mid + right) / 2;
if (cal(mid) < cal(midmid)) left = mid; else right = midmid;
2 100 200
-74.4291 -178.8534
题意:即求最小值!
代码如下:
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #define EPS 1e-7 //开始的时候设置成五位 一直wa using namespace std; int y; double Calc(double x) { return 6*pow(x,7.0)+8*pow(x,6.0)+7*pow(x,3.0)+5*pow(x,2.0)-y*x; } double Solve(void) { double Left, Right; double mid, midmid; double mid_value, midmid_value; Left = 0; Right = 100; while (Right - Left > EPS ) { mid = (Left + Right) / 2; midmid = (mid + Right) / 2; mid_value = Calc(mid); midmid_value = Calc(midmid); if (mid_value < midmid_value) Right = midmid; else Left = mid; } return Left; } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d",&y); printf("%.4lf\n",Calc( Solve())); } return 0; }
赞!不懂请评论提出!