牛顿迭代(开方,板子)

就是正常的牛顿迭代求开方,牛顿迭代日后更新

开方:

#include <bits/stdc++.h>
using namespace std;
const double eps=1e-9;
double sqr(double x)
{
    double k=x;
    while(k*k-x>eps)
        k=0.5*(k+x/k);
    return k;
}

int main()
{
    double x;
    while(cin>>x)
        printf("%.9f\n",sqr(x));
    return 0;
}

 

posted @ 2018-03-11 11:05  啦啦啦天啦噜  阅读(159)  评论(0)    收藏  举报