#include<stdio.h> #include<math.h> #include<string.h> void main() { double x0,x1,s; scanf("%lf",&s); x0=s/2.0; x1=(x0+s/x0)/2.0;//通过迭代法缩小x0和x1的差距,让x1最终靠近平方根 while(abs(x0-x1)>=1e-5) { x0=x1; x1=(x0+s/x0)/2.0; } printf("%.3lf",x1); } |
Double click to view unformatted code.