求平方根的巴比伦算法

#include<iostream>
using namespace std;
double guess(int n)
{
       double guess=static_cast<double>(n)/2;
       int i=0;
       while(i<10)
       {
       double r=n/guess;
       guess=(guess+r)/2;
       ++i;
       }
       return guess;
}
int main()
{
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    cout<<guess(3)<<endl;
    system("pause");
}
posted @ 2010-10-21 18:27  hailong  阅读(594)  评论(0编辑  收藏  举报