这是想告诉我什么?
What is this trying to tell me?
我正在看 RG Dromey 的《如何用计算机解决它》一书。我被困在一个试图解释循环终止的句子上。问题来了:
Suppose we wish to establish that an array of n
elements is in strictly ascending order (i.e. a[1] < a[2] < ... < a[n]
) . To do this we can use the following instructions:
a[n+1] := a[n];
i := 1;
while a[i] < a[i+1] do i := i+1
(现在如果n
是元素个数,那么i
在这种情况下代表什么?它代表值吗?)
If n
was assigned the value 5 and the data set was 2, 3, 5, 11, 14, then the first assignment prior to the loop would result in the array configuration below:
(这是我感到困惑的地方。)
a[1] a[2] a[3] a[4] a[5] a[6]
2 3 5 11 14 14
The two 14's guarantee that the test a[i] < a[i+1]
will be false when i = n
and so the loop will terminate correctly when i = n
if not before.
(这令人困惑。)
i 只是索引
我:= 1;表示 i 等于 1
i := i+1 表示将 1 添加到 i
n = 5
a[5] = 14
a[5+1] = a[6] = 14
14 < 14 为假 - 循环终止
我正在看 RG Dromey 的《如何用计算机解决它》一书。我被困在一个试图解释循环终止的句子上。问题来了:
Suppose we wish to establish that an array of
n
elements is in strictly ascending order (i.e.a[1] < a[2] < ... < a[n]
) . To do this we can use the following instructions:a[n+1] := a[n]; i := 1; while a[i] < a[i+1] do i := i+1
(现在如果n
是元素个数,那么i
在这种情况下代表什么?它代表值吗?)
If
n
was assigned the value 5 and the data set was 2, 3, 5, 11, 14, then the first assignment prior to the loop would result in the array configuration below:
(这是我感到困惑的地方。)
a[1] a[2] a[3] a[4] a[5] a[6] 2 3 5 11 14 14
The two 14's guarantee that the test
a[i] < a[i+1]
will be false wheni = n
and so the loop will terminate correctly wheni = n
if not before.
(这令人困惑。)
i 只是索引
我:= 1;表示 i 等于 1
i := i+1 表示将 1 添加到 i
n = 5
a[5] = 14
a[5+1] = a[6] = 14
14 < 14 为假 - 循环终止