标签:
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #define sc(x) scanf("%d",&(x)) 5 #define sc1(x) scanf("%lf",&(x)) 6 #define pf(x) printf("%.4lf\n", x) 7 using namespace std; 8 int T; 9 double y, front, rear, mid; 10 double r(double x) 11 { 12 return (42*pow(x, 6) + 48*pow(x, 5) + 21*pow(x, 2) + 10*x); 13 } 14 double f(double x) 15 { 16 return (6*pow(x, 7) + 8*pow(x, 6) + 7*pow(x, 3) + 5*pow(x, 2) - y*x); 17 } 18 double Find() 19 { 20 front = 0, rear = 100, mid; 21 while((rear - front) > 1e-6) 22 { 23 mid = (front + rear) / 2.0; 24 if(r(mid) < y) 25 front = mid + 1e-7; 26 else 27 rear = mid - 1e-7; 28 } 29 return (front + rear) / 2.0; 30 } 31 int main() 32 { 33 sc(T); 34 while(T--) 35 { 36 sc1(y); 37 if(r(100) > y) 38 pf(f(Find())); 39 else 40 pf(f(100)); 41 } 42 return 0; 43 }
函数为 F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x;
那么它的导函数为 F‘(x) = 42 * x^6+48*x^5+21*x^2+10*x-y;
设计函数,求函数 G‘(x) = 42 * x^6+48*x^5+21*x^2+10*x 的值,将其减去输入的y值;
可以看出,函数G‘(x)为单调递增函数;
如果对于100,G‘(x) - y <= 0,那么对于任意数【0-100】,G‘(x)都<=0,则F(100)为整个函数最小值;
否则,我们进行二分查找;
转载:http://www.xuebuyuan.com/492486.html
第一次深刻体会到二分思想
标签:
原文地址:http://www.cnblogs.com/tyx0604/p/4395736.html