本文實例講述了Python基于二分查找實現求整數平方根的方法。分享給大家供大家參考,具體如下:
x=int(raw_input('please input a int:'))if x<0: retrun -1low=0high=xans=(low+high)/2.0sign=answhile ans**2 !=x: if ans**2>x: high=ans else: low=ans ans=(low+high)/2.0 if sign==ans: breakprint ans更多關于Python相關內容可查看本站專題:《Python正則表達式用法總結》、《Python數據結構與算法教程》、《Python Socket編程技巧總結》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
新聞熱點
疑難解答
圖片精選