我试图在以下代码中找到循环不变量:
查找最接近的对 Iter(A) :
# Precondition: A is a non-empty list of 2D points and len(A) > 1.
# Postcondition: Returns a pair of points which are the two closest points in A.
min = infinity
p = -1
q = -1
for i = 0,...,len(A) - 1:`=
for j = i + 1,...,len(A) - 1:
if Distance(A[i],A[j]) < min:
min = Distance(A[i],A[j])
p = i
q = j
return (A[p],A[q])
我认为循环不变量是 min = Distance(A[i],A[j]) 所以 A 中最近的点是 A[p] 和 a[q] 。我试图证明程序的正确性。在这里,我想通过让 i 成为一些常数来证明内循环,然后一旦我证明了内循环,就用它的循环不变量替换它并证明外循环。顺便说一句,这是家庭作业。任何帮助都感激不尽。