首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

一道二分法求解的有关问题,为什么总是超时呢?求高手解答

2012-04-17 
一道二分法求解的问题,为什么总是超时呢?求高手解答!Problem DescriptionNow,given the equation 8*x^4 +

一道二分法求解的问题,为什么总是超时呢?求高手解答!
Problem Description
Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can you find its solution between 0 and 100;
Now please try your lucky. 
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 a real number Y (fabs(Y) <= 1e10); 
Output
For each test case, you should just output one real number(accurate up to 4 decimal places),which is the solution of the equation,or “No solution!”,if there is no solution for the equation between 0 and 100. 
Sample Input
2
100
-4

Sample Output
1.6152
No solution!


我的代码如下:
#include<stdio.h>
#include<math.h>
int main()
{
  int t,i,m;
  float y,x0,x1,x2,f0,f1,f2;
  scanf("%d",&t);
  for(i=0;i<t;i++)
  {
  m=0;
  x1=0.0;
  x2=100.0;
  scanf("%f",&y);
  f1=8.0*x1*x1*x1*x1+7.0*x1*x1*x1+2.0*x1*x1+3.0*x1+6.0-y;
  f2=8.0*x2*x2*x2*x2+7.0*x2*x2*x2+2.0*x2*x2+3.0*x2+6.0-y;
  do
  {
  if(y<6||y>807020288){m=1;break;}
  x0=(x1+x2)/2.0;
  f0=8.0*x0*x0*x0*x0+7.0*x0*x0*x0+2.0*x0*x0+3.0*x0+6.0-y;
  if((f1*f0)<0)
  {
  x2=x0;
  f2=f0;
  }
  else
  {
  x1=x0;
  f1=f0;
  }
  }
  while(fabs(f0)>=1e-4);
  if((x0<0&&x0>100)||m==1)printf("No solution!\n");
  else printf("%.4f\n",x0);
  }
  return 0;
}
交到OJ上去老是超时,为什么啊?要怎么修改?

[解决办法]
pow和double可能是精度的问题,这个可能会影响二分的最后一次位置吧。
[解决办法]
我看关键是double吧
[解决办法]
精度问题。

热点排行