递归求方根

#include <stdio.h>
double mysqrt(double a,double x0)
{
double x1;
x1=(x0+a/x0)/2.0;
if(fabs(x1-x0)>0.0000000000001)
return mysqrt(a,x1);
//第一个参数为始终是a,2x1=x0+x0/2,当x0和x1近似相等时,x1*x1=a,所以参数a固定,返回x1
else
return x1;
}
int main()
{
double x;
scanf("%lf",&x);
printf("The sqrt of %f=%f\n",x,mysqrt(x,1.0));
return 0;
}

posted @ 2012-04-14 13:39  加拿大小哥哥  阅读(186)  评论(0编辑  收藏  举报