二分查找-平方根

设计函数int sqrt(int x),计算 xx 的平方根。

输入格式

输入一个 整数 xx,输出它的平方根。直到碰到文件结束符(EOF)为止。

输出格式

对于每组输入,输出一行一个整数,表示输入整数的平方根。

样例输入

1
2
3
4
5
6
7
8
9
样例输出

1
1
1
2
2
2
2
2
3
实现代码
#include
#include
using namespace std;
int sqrt(int x)  
 
    double t=1.0;       
    while(abs(t*t-x)>1e-5)       
         
        t=(x/t+t)/2.0;       
         
    return t;    
 
int main()
{
int x;
while(cin>>x)
cout<<sqrt(x)<<endl;
return 0;
}
原文地址:https://www.cnblogs.com/kuroko-ghh/p/9363381.html