擅长:python、mysql、java
<p>看看其他的答案。解决方案是:</p>
<pre><code>return int(middle + 0.5)
</code></pre>
<p>向最接近的整数舍入。你知道吗</p>
<p>当然,更大的问题是,在计算整数平方根时,要将中间值转换为浮点值。与Python int相比,float的范围非常有限。请尝试以下操作:</p>
<pre><code>def int_sqrt(n):
"""Return integer square root of integer ``n``.
"""
x = n
y = (x + 1) >> 1
while y < x:
x = y
y = (x + n // x) >> 1
return x
</code></pre>
<p>例如,在<code>int_sqrt(2**12345-1)</code>上运行它,它应该返回一个1858位的数字。你知道吗</p>