其他分享
首页 > 其他分享> > 排列硬币

排列硬币

作者:互联网

# 排列硬币

# 暴力求解
def func1(n: int):
for i in range(1, n + 1):
n -= i
if n < i:
return i


# 二分求解
def func2(n: int):
low = 1
high = n
while low <= high:
mid = (low + high) // 2
if (mid + 1) * mid / 2 == n:
return mid
elif (mid + 1) * mid / 2 < n:
low = mid + 1
else:
high = mid - 1


# 牛顿迭代求解 (i + n/i)/2 <= n
def func3(x: int):
return func(1, x)


def func(x: int, n: int):
res = (x + (2*n-x)/x) / 2
if res == x:
return x
else:
return func(res, n)


print(func1(10))
print(func2(10))
print(func3(10))

标签:排列,return,硬币,int,mid,high,low,def
来源: https://www.cnblogs.com/bigcoolcool/p/16270149.html