hdu2899Strange fuction(解方程+二分)

Strange fuction

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


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

给出y,求出F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x的最小值,x在0~100之间。

需要先求一下他的导函数42x^6+48x^5+21x^2+10x-y。最小值就是这个导函数为0

这样就可以用二分求一下了,具体看代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int inf=0x3f3f3f3f;
 4 double y;
 5 double f(double x)
 6 {
 7     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;
 8 }
 9 double f2(double x)
10 {
11     return 42*x*x*x*x*x*x+48*x*x*x*x*x+21*x*x+10*x;
12 }
13 int main()
14 {
15     int t;
16     while(~scanf("%d",&t))
17     {
18         while(t--)
19         {
20             
21             scanf("%lf",&y);
22             double l=0.0,r=100.0,mid=50.0;
23             while(fabs(f2(mid)-y)>1e-5)
24             {
25                 if(f2(mid)<y)
26                 {
27                     l=mid;
28                     mid=(l+r)/2.0;
29                 }
30                 if(f2(mid)>y)
31                 {
32                     r=mid;
33                     mid=(l+r)/2.0;
34                 }
35                 
36             }
37             printf("%.4lf
",f(mid));
38         }
39     }
40     return 0;
41 }
原文地址:https://www.cnblogs.com/fqfzs/p/9943040.html