【发布时间】:2023-10-04 09:04:01
【问题描述】:
int i, temp;
a is an array of integers [1...100]
i = 1;
while i < 100
if a[i] > a[i+1]
temp = a[i]
a[i] = a[i+1]
a[i+1] = temp
i = i+1
我无法理解如何找到循环不变量并为它们编写正式语句。因此,循环不变量只是在循环的每次迭代之前和之后立即为真的条件。看起来代码正在执行交换:如果数组中的以下元素大于当前元素,则切换位置。我的意思是从循环不变量的定义来看,它听起来真的是 i
【问题讨论】:
-
“Accelerated C++”一书帮助我理解了“循环不变”术语。也许你应该试一试。它位于“第 2 章:循环和计数”中。
-
这样看:你写循环的时候在想什么?
标签: algorithm loops proof loop-invariant proof-of-correctness