我有三个整数:A
、B
、C
我需要找到整数 X
,它是最接近 C
。如果N
是任意自然数,则A
、B
和X
应满足以下等式:
A*B*X=sqrt(N)
你能帮忙做算法吗?
最佳答案
我们可以对N
的所有可能值进行二分查找,然后比较X = sqrt(N)/(A*B))
的对应值来决定哪一半进行搜索。
在 python 中的可能实现可能是 -
A = 5
B = 2
C = 3
left = -10000000000000
right = 10000000000000 #assuming that's the maximum value N can take
while right-left>1:
N = (left+right)//2
X = N**.5/(A*B)
if X>C:
right = N
else:
left = N
N = (left+right)//2
print(N)
在这种情况下输出:900
关于algorithm - 找到最接近给定自然数,满足非线性条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47772284/