迭代平方根(c++)
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
double x;
cin >> x;
double a1 = x, a2 = (a1 + x/a1)/2.0;
while (abs(a1-a2) >= 0.000001) {
a1 = a2;
a2 = (a1 + x/a1)/2.0;
}
printf("%.3f\n", a2);
return 0;
}
登录后发布评论
暂无评论,来抢沙发