游客 Signup | Login
中文 | En

1172 - C语言6.20

用迭代法求。求平方根的迭代公式为:

<img src="http://tk.hustoj.com:80/upload/pimg1209_2.gif" width="118" height="49" align="middle" alt="" />

当前后两次迭代求出的x的差的绝对值小于10<sup>-5</sup>时就能够得出解。

Input

一个正实数a。

Output

输出迭代法求出的a的平方根,保留4位小数。

请注意行尾输出换行。

Examples

Input

12

Output

3.4641

Solution C

#include<stdio.h>
#include<math.h>
int main(){
float a,x1,x2;
scanf("%f",&a);
x2=a;
  while(fabs(x1-x2)>1e-5){
  x1=x2;
  x2=(x1+a/x1)/2.0;
  }
printf("%.4f\n",x1);
return 0;
}

Solution C++

#include <stdio.h>
#include <math.h>
int main() {
	float a, x, newx;
	scanf("%f", &a);
	newx = a;
	do {
		x = newx;
		newx = 0.5 * (x + a / x);
	} while (fabs(x - newx) >= 1e-5);
	printf("%.4f\n", x);
	return 0;
}

Time Limit 1 second
Memory Limit 32 MB
Discuss Stats
上一题 下一题