HDU2199 ( 二分 )

转化为求给定的函数与X轴的交点

二分即可

View Code
 1 #include<stdio.h>

 2 #include<math.h>

 3 

 4 double func( double x,double y ){

 5     double ans;

 6     ans=8*pow( x,4.0 )+7*pow( x,3.0 )+2*pow( x,2.0 )+3*x+6.0-y;

 7     return ans;

 8 }

 9 int judge( double y ){

10     double t1,t2;

11     t1=func( 0,y ),t2=func( 100,y );

12     if( t1*t2<=0 ) return 1;

13     else return -1;//no solution

14 }

15 

16 int main(){

17     int T;

18     scanf("%d",&T);

19     while( T-- ){

20         double y;

21         scanf("%lf",&y);

22         if( judge( y )==-1 ){

23             printf("No solution!\n");

24             continue;

25         }

26         double left,right,mid;

27         left=0,right=100;

28         double delta=1e-8;

29         while( left<right-delta ){

30             mid=(left+right)/2.0;

31             if( func( mid,y )==0 ) break;

32             if( func( mid,y )>0 ) right=mid-delta;

33             else left=mid+delta;

34         }

35         printf("%.4lf\n",mid);

36     }

37     return 0;

38 }

 

你可能感兴趣的:(HDU)