Strange fuction--hdu2899

9902 ワード

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.
 
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 }

 
 次は私が学んだばかりの3つの方法です.
 
 
 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 }