求一个数的算术平方根

  • Post author:
  • Post category:其他


写一段程序求出一个数的算术平方根:

#include<iostream>
using namespace std;
#define e 0.001
double sqrt(double a)
{
		double x,y;
		x=a/2;
		y=x+1+e;
		while(x-y>e||y-x>e)
		{
				y=x;
				x=(x+a/x)/2;
		}
		return x;
}
int main()
{
		double a;
		cin>>a;
		cout<<sqrt(a);
		return 0;
}