当前位置:Gxlcms > Python > Python基于二分查找实现求整数平方根

Python基于二分查找实现求整数平方根

时间:2021-07-01 10:21:17 帮助过:10人阅读

本文实例讲述了Python基于二分查找实现求整数平方根的方法。分享给大家供大家参考,具体如下:

  1. x=int(raw_input('please input a int:'))
  2. if x<0:
  3. retrun -1
  4. low=0
  5. high=x
  6. ans=(low+high)/2.0
  7. sign=ans
  8. while ans**2 !=x:
  9. if ans**2>x:
  10. high=ans
  11. else:
  12. low=ans
  13. ans=(low+high)/2.0
  14. if sign==ans:
  15. break
  16. print ans


更多 Python基于二分查找实现求整数平方根相关文章请关注PHP中文网!

人气教程排行