Can you solve this equation?

#include<stdio.h>
#include<math.h>
double fun(double x)
{
   return 8*x*x*x*x + 7*x*x*x + 2*x*x + 3*x + 6 ;
}
int main( )
{
  int N,flag;
  double Y,a,b,mid;
  scanf("%d",&N);
  while(N--)
  {
   flag=0;
   scanf("%lf",&Y);
   a=0;b=100;mid=(a+b)/2;
   if(Y<6||Y>fun(100))  //刚开始的时侯TLE了,原来是这里忘记考虑Y>fun(100)
     {
     printf("No solution!\n");
     flag=1;
     continue;
     }
   while(fabs(fun(mid)-Y)>1e-4)
   {
     
     if(fun(mid)-Y>=0)
     b=mid;
     else
     a=mid;
     mid=(a+b)/2.0;
     
     //printf("%.4lf\n",mid);
    // printf("%.4lf\n",fun(mid)-Y)
   }
   if(!flag)
   printf("%.4lf\n",mid);
  }
   return 0;
}
  //超时的原因,1.死循环。2.算法太差。
                               

posted on 2011-05-03 00:36  more think, more gains  阅读(230)  评论(0编辑  收藏  举报

导航