1 #include <stdio.h> 2 #define eps 1e-6 3 4 double fsqrt(double a) 5 { 6 double x=a,t; 7 /* 迭代x*/ 8 do{ 9 t=x; 10 x = 0.5*(x+a/x); 11 }while (t-x>eps); 12 13 return x; 14 } 15 16 int main() 17 { /* 1. 定义a,符号sign*/ 18 double a,sign=1; 19 scanf("%lf",&a); 20 /* 2. 取正 */ 21 if (a<0){ 22 sign = -1; 23 a *= sign; 24 } 25 /* 3 打印平方根x*/ 26 printf("%.2f",fsqrt(a)*sign); 27 return 0; 28 }