算法一:#include <stdio.h>#include <stdlib.h>double __sqrt(double n){ double root=n/2.0; while( fabs(root*root-n)>0.000001 ) root = (root+n/root)/2.0; return root; } int main(int argc, char *argv[]){ int n; for(n=0; n<=100; n++) printf("sqrt(%3d) = %lf\n", n, __sqrt((double) n)); system("PAUSE"); return 0;}算法二:#include<math.h>#include<stdio.h>int main(){double x,x0; x=1.5; do{ x0=x; x=x/2+1/x; } while (fabs(x-x0)>=1e-10);printf("sqrt(2)=%1.10f\n",x);return 0;}

评论