Strange fuction
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4538 Accepted Submission(s):
3261
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
2
100
200
Sample Output
-74.4291
-178.8534
分析:这个题是要求方程的最小值,首先我们来看一下他的导函数: F’(x) = 42 * x^6+48*x^5+21*x^2+10*x-y(0 <= x <=100)
很显然,导函数是递增的,那么只要求出其导函数的零点就行了,下面就是用二分法求零点!
1 #include<stdio.h> 2 #include<math.h> 3 double hs(double x,double y) 4 { 5 return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*x*x-y*x;//定义一个求函数值得函数 6 } 7 double ds(double x,double y) 8 { 9 return 42*pow(x,6)+48*pow(x,5)+21*pow(x,2)+10*x-y;//定义一个求导数的函数 10 } 11 int main() 12 { 13 int a; 14 scanf("%d",&a); 15 while(a--) 16 { 17 double b,x,y,z; 18 scanf("%lf",&b); 19 x=0.0; 20 y=100.0; 21 do 22 { 23 z=(x+y)/2; 24 if(ds(z,b)>0) 25 y=z; 26 else 27 x=z; 28 }while(y-x>1e-6);//求出一定精度内导数为0的大约值 29 printf("%.4lf ",hs(z,b)); 30 } 31 return 0; 32 }
下面是我刚学的三分法:
1 #include<stdio.h> 2 #include<math.h> 3 double hs(double x,double y) 4 { 5 return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*x*x-y*x; 6 } 7 int main() 8 { 9 int n; 10 scanf("%d",&n); 11 while(n--) 12 { 13 double l,r,mid,midmid,a; 14 scanf("%lf",&a); 15 l=0.0; 16 r=100.0; 17 do 18 { 19 mid=(l+r)/2; 20 midmid=(mid+r)/2; 21 if(hs(mid,a)>hs(midmid,a)) 22 l=mid; 23 else 24 r=midmid; 25 }while(r-l>1e-6); 26 printf("%.4lf ",hs(mid,a)); 27 } 28 return 0; 29 }