Description
用迭代法求 。求平方根的迭代公式为: X[n+1]=1/2(X[n]+a/X[n]) 要求前后两次求出的得差的绝对值少于0.00001。输出保留3位小数
Input
X
Output
X的平方根
Sample Input
4
Sample Output
2.000
代码:
#include<stdio.h> #include<math.h> int main() { float X,c,x1,x2; scanf("%f",&X); x1=X; for(c=1;c>=0.00001; ) { x1=x2; x2=(x1+X/x1)/2; c=fabs(x2-x1); } printf("%.3f",x2); return 0; }
-
oj刷题:猜灯谜
:下一篇 »