我实现了最接近的对算法,我试图找出随机大小的随机点列表中哪些点最接近。我没有得到有问题的分数,我要么让它再次返回距离,要么就是没有。我对任何批评都持开放态度,但我很困惑为什么我会遇到这些打嗝,因为理论上当距离是一个新的低点,而温度表示这个,然后我可以追加或设置一对点作为变量或在列表中。
import math
from math import*
from random import *
from graphics import*
def ClosestPair(P,N):
#Closest Pair of Points are compared, and the distance between the two pairs
#is output as a number.
#
ret = []
d = math.inf
for i in range(N):
for j in range(i+1,N):
temp = -1
d = min(d,sqrt((((P[i].getX() - P[j].getX())**2) + ((P[i].getY() - P[j].getY())**2))))
if temp < d:
temp = d
else:
ret.append(P[i])
ret.append(P[j])
return ret
def main():
#X&Y is lists that get filled with N values of N size
#X&Y are appended as the X's and Y's of a Point to Array.
#Then If ClosestPair = 0 prints closest pair saying points are
#overlapping
#Else prints the Distance and Pair of Points that are Closest
x = []
y = []
array = []
print('Enter A Size for N')
n = int(input('N = '))
for a in range(n):
x.append(randrange(n))
y.append(randrange(n))
array.append(Point(x[a],y[a]))
#print(array)
if ClosestPair(array,n) == 0:
print("The Closest Pair of Points are On top of One another!")
else:
print("The Distance between the Closest Points is: ", ClosestPair(array,n), "Points Away")
main()
答案 0 :(得分:0)
内循环错误。它应该是这样的:
import
此外,您的函数现在返回一对。将它与0进行比较是没有意义的。如果您想要或重新计算距离,请返回距离和点数。