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


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

x=int(raw_input('please input a int:'))
if x<0: 
  retrun -1
low=0
high=x
ans=(low+high)/2.0
sign=ans
while ans**2 !=x:
  if ans**2>x:
    high=ans
  else:
    low=ans
  ans=(low+high)/2.0
  if sign==ans:
    break
print ans

更多关于Python相关内容可查看本站专题:《Python正则表达式用法总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

Python下载指定页面上图片的方法
本文实例讲述了Python下载指定页面上图片的方法。分享给大家供大家参考,具体如下:#!/usr/bin/python#coding:utf8importreimporturllibdefgetHtml(url):page=urllib.urlopen(u

Python安装使用命令行交互模块pexpect的基础教程
一、安装1、安装easy_install工具wgethttp://peak.telecommunity.com/dist/ez_setup.pypythonez_setup.py安装easy_install工具(这个脚本会自动去官网搜索下载并安装)pythonez_set

Python中遍历字典过程中更改元素导致异常的解决方法
先来回顾一下Python中遍历字典的一些基本方法:脚本:#!/usr/bin/pythondict={"a":"apple","b":"banana","o":"orange"}print"##########dict######################"foriindict:print"dict[%s]=