杭电2899--Strange fuction(二、三分)

Strange fuction

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


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
 

 

Author
Redow
 

 

Recommend
lcy   |   We have carefully selected several similar problems for you:  2289 2298 3400 2446 1399 
RE: 求最值问题。
//三分法:
 1 #include <cstdio>
 2 #include <iostream>
 3 using namespace std;
 4 double f(double x, double y)
 5 {
 6     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;
 7 }
 8 int main()
 9 {
10     int t; double m;
11     scanf("%d", &t);
12     while(t--)
13     {
14         scanf("%lf", &m);
15         double l_min, r_max, min = 0.0, max = 100.0;
16     //    printf("%.4lf %.4lf
", min, max);
17         while(max - min > 1e-8)
18         {
19             l_min = (2 * min + max) / 3.0;
20             r_max = (min + 2 * max) / 3.0;
21             if(f(l_min, m)>f(r_max, m)) min = l_min;
22             else max = r_max; 
23         }
24     //    printf("%.4lf %.4lf
", min, max);
25         printf("%.4lf
",f((min + max) / 2.0, m));
26     }
27     return 0;
28 } 

//闲来无事, 敲敲手生的二分(大同小异, 一个比较值, 一个比较导数);

 1 #include <cstdio>
 2 #include <iostream>
 3 using namespace std;
 4 double f(double x, double y)
 5 {
 6     return 42*x*x*x*x*x*x+48*x*x*x*x*x+21*x*x+10*x-y;
 7 }
 8 double F(double x, double y)
 9 {
10     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; 
11 }
12 int main()
13 {
14     int t;
15     scanf("%d", &t);
16     while(t--)
17     {
18         double m, min = 0.0, max = 100.0, mid;
19         scanf("%lf", &m);
20         while(max - min > 1e-8)
21         {
22             mid = (max + min) / 2.0;
23             if(f(mid, m) <= 0) min = mid;
24             else max = mid;
25         }
26         printf("%.4lf
", F((min + max) / 2.0 , m));
27     }
28     return 0;
29 } 
原文地址:https://www.cnblogs.com/soTired/p/4701939.html